All pastes #906125 Raw Edit

Something

public text v1 · immutable
·published 2008-02-16 10:18 UTC
rendered paste body
public static T[] RandomShuffle<T>(IEnumerable<T> collection, Random
randomGenerator)

{

// We have to copy all items anyway, and there isn't a way to produce the
items

// on the fly that is linear. So copying to an array and shuffling it is an
efficient as we can get.

if (collection == null)

throw new ArgumentNullException("collection");

if (randomGenerator == null)

throw new ArgumentNullException("randomGenerator");

T[] array = Algorithms.ToArray(collection);

int count = array.Length;

for (int i = count - 1; i >= 1; --i) {

// Pick an random number 0 through i inclusive.

int j = randomGenerator.Next(i + 1);

// Swap array[i] and array[j]

T temp = array[i];

array[i] = array[j];

array[j] = temp;

}

return array;

}