Skip to content

Instantly share code, notes, and snippets.

@ThirdPartyNinjas
Created October 10, 2020 18:20
Show Gist options
  • Save ThirdPartyNinjas/4cec72adaa542407623b0eba558d0a74 to your computer and use it in GitHub Desktop.
Save ThirdPartyNinjas/4cec72adaa542407623b0eba558d0a74 to your computer and use it in GitHub Desktop.
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