Shuffle
Shuffle an ArrayList in O(n) time (fastest possible way in theory and practice!)
Source
public static ArrayList Shuffle(this ArrayList list)
{
var r = new Random((int)DateTime.Now.Ticks);
for (int i = list.Count - 1; i > 0; i--)
{
int j = r.Next(0, i - 1);
var e = list[i];
list[i] = list[j];
list[j] = e;
}
return list;
}
Example
ArrayList a = new ArrayList() { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 };
a.Shuffle();
Author: Maarten van Duren
Submitted on: 20 mrt. 2013
Language: C#
Type: System.Collections.ArrayList
Views: 5679