Shuffle
public static IEnumerable Shuffle(this IEnumerable items)
{
var random = new Random();
return items.OrderBy(x => random.Next());
}
EDIT: It seems there are several issues with the above implementation. Here is an improved version based @LukeH's code and comments from @ck and @Strilanc.
private static Random _rand = new Random();
public static IEnumerable Shuffle(this IEnumerable source)
{
var items = source == null ? new T[] { } : source.ToArray();
var count = items.Length;
while(count > 0)
{
int toReturn = _rand.Next(0, count);
yield return items[toReturn];
items[toReturn] = items[count - 1];
count--;
}
}