Shuffling lists in C# – an extensionmethod
This is an extensionmethod for shuffling generic lists.
I absolutely LOVE generics!
Back in the days of .net 1.0 ("yaaawn" - I know, but bear with me...) there was a class called CollectionBase which had an ArrayList (like a "List<Object>") inside, and to make your own strongly typed collection you would inherit from CollectionBase and implement a typed Setter and Getter. No more of that - phew!! Thank you Microsoft!!
The sample project gives a demonstration of shuffling four different List types:
List<int> _numbers; List<string> _names; List<Color> _colors; List<Card> _cards;
How to use it
When you've added a reference to the ListShufflerExtensionMethods class, all Lists get a Shuffle() method. So you can call Shuffle on any type of List.
_numbers.Shuffle(); _names.Shuffle(); _colors.Shuffle(); _cards.Shuffle();
The Shuffle() method has an optional parameter numberOfTimesToShuffle where you can specify how many times to shuffle. The default value is 5.
Demonstration
Complete code
Here you can see the implementation. Extensionmethods have to be public, static methods on a public static class. Using the "this" keyword on the first parameter passed to a method you effectively add the method to that type (see line 16 below).
/// <summary> /// Class for shuffling lists /// </summary> /// <typeparam name="T">The type of list to shuffle</typeparam> public static class ListShufflerExtensionMethods { //for getting random values private static Random _rnd = new Random(); /// <summary> /// Shuffles the contents of a list /// </summary> /// <typeparam name="T">The type of the list to sort</typeparam> /// <param name="listToShuffle">The list to shuffle</param> /// <param name="numberOfTimesToShuffle">How many times to shuffle the list /// by default this is 5 times</param> public static void Shuffle<T>(this List<T> listToShuffle, int numberOfTimesToShuffle = 5) { //make a new list of the wanted type List<T> newList = new List<T>(); //for each time we want to shuffle for (int i = 0; i < numberOfTimesToShuffle; i++) { //while there are still items in our list while (listToShuffle.Count > 0) { //get a random number within the list int index = _rnd.Next(listToShuffle.Count); //add the item at that position to the new list newList.Add(listToShuffle[index]); //and remove it from the old list listToShuffle.RemoveAt(index); } //then copy all the items back in the old list again listToShuffle.AddRange(newList); //and clear the new list //to make ready for next shuffling newList.Clear(); } } }
Source code
ListShufflerExtensions.cs (right click > save as...)
March 17th, 2014 at 07:56
Hello,
Your List Shuffler class helped me a lot. Thanks for sharing the code.
Regards,
Nuthan Gowda
March 17th, 2014 at 07:59
Great to hear it!
Thanks for your feedback
/Jakob
May 31st, 2014 at 11:39
I came across this very simple way of accomplishing this task awhile back and felt I should share.
It too will work with any list.
if (myList.Count >=1)
myList = myList.OrderBy(c => Guid.NewGuid()).ToList();
June 1st, 2014 at 20:33
Very nice - thanks for sharing!