ShfuffleExtension.cs 577 B

12345678910111213141516171819
  1. using System.Collections.Generic;
  2. // simple shuffle realization
  3. public static class ShfuffleExtension {
  4. private static readonly System.Random RandomGenerator = new System.Random();
  5. public static void Shuffle<T>(this IList<T> shuffleList)
  6. {
  7. int count = shuffleList.Count;
  8. while (count > 1)
  9. {
  10. count--;
  11. int randomValue = RandomGenerator.Next(count + 1);
  12. T value = shuffleList[randomValue];
  13. shuffleList[randomValue] = shuffleList[count];
  14. shuffleList[count] = value;
  15. }
  16. }
  17. }