Randomize a list of strings in .net
December 09, 2010
C#
Snippet
Need to randomize your list or array? Well, here's a snippet for you that will return the list with the items in a random order. It can easily be changed to sort other types of lists (or arrays) as well.
private static Random rng = new Random();
public static List<string> Randomize(List<string> list) {
byte[] order = new byte[list.Count];
rng.NextBytes(order);
string[] tmp = list.ToArray();
Array.Sort(order, tmp);
return tmp.ToList<string>();
}
Related posts: