Created
October 10, 2020 18:20
-
-
Save ThirdPartyNinjas/4cec72adaa542407623b0eba558d0a74 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Collections.Generic; | |
public class ShuffleBag<T> | |
{ | |
public ShuffleBag() : this(-1) { } | |
public ShuffleBag(Int32 seed) | |
{ | |
if(seed < 0) | |
random = new Random(); | |
else | |
random = new Random(seed); | |
activeList = new List<T>(); | |
inactiveList = new List<T>(); | |
} | |
public void Reset() | |
{ | |
activeList.AddRange(inactiveList); | |
inactiveList.Clear(); | |
} | |
public void Add(T t, int copies = 1) | |
{ | |
for(int i = 0; i < copies; i++) | |
{ | |
activeList.Add(t); | |
} | |
} | |
public void Add(IEnumerable<T> enumerable) | |
{ | |
foreach(var t in enumerable) | |
{ | |
Add(t); | |
} | |
} | |
public T Next() | |
{ | |
int selectedIndex = random.Next(activeList.Count); | |
T t = activeList[selectedIndex]; | |
activeList.RemoveAt(selectedIndex); | |
inactiveList.Add(t); | |
return t; | |
} | |
Random random; | |
List<T> activeList; | |
List<T> inactiveList; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment