Advertisement
ChestnutGames

C# extension method for shuffling IList collections

Aug 20th, 2015
258
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 0.21 KB | None | 0 0
  1. public static void Shuffle<T>(this IList<T> list)
  2. {
  3.     int n = list.Count;
  4.    
  5.     while (n > 1)
  6.     {
  7.         n--;
  8.         int k = Random.Range(0, n+1);
  9.            
  10.         T value = list[k];
  11.         list[k] = list[n];
  12.         list[n] = value;
  13.     }
  14. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement