mirror of
https://github.com/kwsch/pkNX.git
synced 2026-03-22 18:24:17 -05:00
File scoped namespaces for all lib projects netstandard2.0 => net6; now uniform. bye netframework!
43 lines
946 B
C#
43 lines
946 B
C#
namespace pkNX.Randomization;
|
|
|
|
/// <summary> Cyclical Shuffled Randomizer </summary>
|
|
/// <remarks>
|
|
/// The shuffled list is iterated over, and reshuffled when exhausted.
|
|
/// The list does not repeat values until the list is exhausted.
|
|
/// </remarks>
|
|
public class GenericRandomizer<T>
|
|
{
|
|
public GenericRandomizer(T[] randomValues)
|
|
{
|
|
RandomValues = randomValues;
|
|
}
|
|
|
|
private readonly T[] RandomValues;
|
|
private int ctr;
|
|
public int Count => RandomValues.Length;
|
|
|
|
public void Reset()
|
|
{
|
|
ctr = 0;
|
|
Util.Shuffle(RandomValues);
|
|
}
|
|
|
|
public T Next()
|
|
{
|
|
if (ctr == 0)
|
|
Util.Shuffle(RandomValues);
|
|
|
|
T value = RandomValues[ctr++];
|
|
ctr %= RandomValues.Length;
|
|
return value;
|
|
}
|
|
|
|
public T[] GetMany(int count)
|
|
{
|
|
var arr = new T[count];
|
|
for (int i = 0; i < arr.Length; i++)
|
|
arr[i] = Next();
|
|
return arr;
|
|
}
|
|
}
|