I think I\'ve settled on this as the most simple and unit-testable method for randomising a list, but would be interested to hear of any improvements.
public sta
Not sure how much of an improvement this is, but would have performance benefits if the list is large and you only need the first few random items.
public static IEnumerable RandomiseList(IList list, int seed)
{
Random random = new Random(seed);
List takeFrom = new List(list);
while (takeFrom.Count > 0)
{
int pos = random.Next(0, takeFrom.Count - 1);
T item = takeFrom[pos];
takeFrom.RemoveAt(pos);
yield return item;
}
}
Removes the need for a temp list or even a temp swap variable.
If I was going to be using this a lot, I'd rewrite it as an extension method.