Extreme Optimization >
User's Guide >
Statistics Library >
Random Numbers >
Random Enumerators
Extreme Optimization User's Guide
User's Guide
Up: Random Numbers Next: Appendices Previous: Quasi-Random Sequences Contents
Random Enumerators and Shufflers
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:
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 Code |
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:
Shuffler.Shuffle(numbers);
for(
int i = 0; i < numbers.Length; i++)
Console.WriteLine(numbers[i]);
| Visual Basic | Copy Code |
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:
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 Code |
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.
Up: Random Numbers Next: Appendices Previous: Quasi-Random Sequences Contents
Copyright 2004-2008,
Extreme Optimization. All rights reserved.
Extreme Optimization, Complexity made simple, M#, and M
Sharp are trademarks of ExoAnalytics Inc.
Microsoft, Visual C#, Visual Basic, Visual Studio, Visual
Studio.NET, and the Visual Studio Logo are registered trademarks of Microsoft Corporation