using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace XnaFan { /// /// Class for shuffling lists /// /// The type of list to shuffle public static class ListShufflerExtensionMethods { //for getting random values private static Random _rnd = new Random(); /// /// Shuffles the contents of a list /// /// The type of the list to sort /// The list to shuffle /// How many times to shuffle the list, by default this is 5 times public static void Shuffle(this List listToShuffle, int numberOfTimesToShuffle = 5) { //make a new list of the wanted type List newList = new List(); //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(); } } } }