ExtensionMethod.NET Home of 880 C#, Visual Basic, F# and Javascript extension methods

Randomize

Randomize the Items in the list

Source

public static void Randomize<t>(this IList<t> target)
{
    Random RndNumberGenerator = new Random();
    SortedList<int, t> newList = new SortedList<int, t>();
    foreach (t item in target)
    {
        newList.Add(RndNumberGenerator.Next(), item);
    }
    target.Clear();
    for (int i = 0; i < newList.Count; i++)
    {
        target.Add(newList.Values[i]);
    }
}

Example

List<int> intList = new List<int>();
            intList.Add(1);
            intList.Add(2);
            intList.Add(3);
            intList.Add(4);
            intList.Add(5);
intList.Randomize(); // this will randomize the items in the list

Author: Ramaraju

Submitted on: 30 jul. 2009

Language: C#

Type: System.Collections.Generic.IList<T>

Views: 4900