This is just a simple extension for lists to enable the shuffling of items within it based on the StackOverflow answer here. This relies on System.Random
so isn’t as random as it could be but is fine for my purposes.
public static IEnumerable<T> Shuffle<T>(this IEnumerable<T> source, Random rng)
{
T[] elements = source.ToArray();
for (int i = elements.Length - 1; i >= 0; i--)
{
int swapIndex = rng.Next(i + 1);
yield return elements[swapIndex];
elements[swapIndex] = elements[i];
}
}
0 Comments