Skip to content

Instantly share code, notes, and snippets.

@AlexanderBrevig
Created February 6, 2013 12:55
Show Gist options
  • Save AlexanderBrevig/4722354 to your computer and use it in GitHub Desktop.
Save AlexanderBrevig/4722354 to your computer and use it in GitHub Desktop.
Extension method for IList<T> for shuffling the content (not thread safe)
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
public static class ListShuffle
{
public static void Shuffle<T>(this IList<T> list)
{
Random rand = new Random();
int i = list.Count;
while (i > 1) {
--i;
int j = rand.Next(i + 1);
T val = list[j];
list[j] = list[i];
list[i] = val;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment