feanz
9/15/2013 - 2:12 PM

Shuffle

Shuffle

public static void Shuffle<T>(this T[] array, Random random)
  {
		int n = array.Length;
		while (n > 1)
		{
			int k = random.Next(n--);
			T temp = array[n];
			array[n] = array[k];
			array[k] = temp;
		}
	}
	
	public static void Shuffle<T>(this IList<T> collection, Random random = null)
	{
		random = random ?? new Random();
		
		int n = collection.Count();
		
		while (n > 1)
		{
			int k = random.Next(n--);
			T temp = collection[n];
			collection[n] = collection[k];
			collection[k] = temp;
		}
	}