The Extreme Optimization Statistics Library for .NET contains a number of utility classes that simplify
working with randomized data. There are classes for shuffling data in random order, and for enumerating the members
of a collection in random order. These classes reside in the Extreme.Statistics.Random namespace.
The Shuffler Class
The Shuffler class lets you shuffle the contents of any
class that implements the IList interface. This includes arrays, ArrayLists, and many other objects from
the System.Collections namespace.
The class has only one static (Shared in Visual Basic) method, which is overloaded. The first variant takes two
parameters: an IList object, and a System.Random object that is used to generate the random
sequence. This can be an instance of the System.Random class supplies with the .NET Framework, or one of
the random number generators from the Extreme.Statistics.Random
namespace:
| C# | Copy |
|---|
int[] numbers = new int[] {1, 2, 3, 4, 5, 6};
Shuffler.Shuffle(numbers, new MersenneTwister());
for(int i = 0; i < numbers.Length; i++)
Console.WriteLine(numbers[i]);
|
| Visual Basic | Copy |
|---|
Dim numbers As Integer() = New Integer() {1, 2, 3, 4, 5, 6}
Shuffler.Shuffle(numbers, New MersenneTwister())
For i As Integer = 0 To numbers.Length - 1
Console.WriteLine(numbers(i))
Next
|
The second variant only takes the IList object to shuffle, and uses a default random number
generator:
| C# | Copy |
|---|
Shuffler.Shuffle(numbers);
for(int i = 0; i < numbers.Length; i++)
Console.WriteLine(numbers[i]);
|
| Visual Basic | Copy |
|---|
Shuffler.Shuffle(numbers)
For i As Integer = 0 To numbers.Length - 1
Console.WriteLine(numbers(i))
Next
|
These methods overwrite the contents of the list with the shuffled data.
Enumerating Collections in Random Order
The RandomEnumerator class iterates over the items
of a collection in a random manner. Every member of the collection is used exactly once.
This class implements the IEnumerator interface. It can be used in the GetEnumerator
method of a collection class to allow the collection to be enumerated in random order using a ForEach
loop.
The RandomizedCollection class is a generic
implementation of such a class. It can work with any IEnumerator object. The following example
enumerates the elements of an array in random order:
| C# | Copy |
|---|
int[] numbers = new int[] {1, 2, 3, 4, 5, 6};
RandomizedCollection collection = new RandomizedCollection(numbers);
foreach (int number in collection)
Console.WriteLine(number);
|
| Visual Basic | Copy |
|---|
Dim numbers As Integer() = New Integer() {1, 2, 3, 4, 5, 6}
Dim collection As RandomizedCollection = New RandomizedCollection(numbers)
For number As Integer In collection
Console.WriteLine(number)
Next
|
The RandomEnumerator and RandomizedCollection classes work most efficiently with
IList objects, but it can be used with any class that implements the IEnumerable interface.
In the latter case, the values in the collection are enumerated before the first random element is returned.