I made a small application to get images from Flickr and wanted to display this in a website in random order. I found this little snippet of code, which shuffles any collection (using generics) into a random collection.
It’s an extension method that extends any collection with a Shuffle method. I just love those extension methods and use them a lot!
public static class Extensions { public static void Shuffle<T>(this IList<T> list) { Random rng = new Random(); int n = list.Count; while (n > 1) { n--; int k = rng.Next(n + 1); T value = list[k]; list[k] = list[n]; list[n] = value; } } }