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 CopyCode imageCopy 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 CopyCode imageCopy 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 CopyCode imageCopy 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

Overview
Introduction
Features
Documentation
QuickStart Samples
Sample Applications
Downloads
Get it now!
Download trial version
How to Buy
Information
Resources
Contact Us
Search

"The Extreme Optimization Statistics Library for .NET is a major boon for those doing statistical work in .NET. I strongly recommend this product."
- Marc Brooks

"I have made it my mission to institutionalize the value of good API design.  I strongly believe that this is key to making developers more productive and happy on our platform. It is clear that you value good API design in your work, and take to heart developer productivity and synergy with the .NET framework."
- Brad Abrams,
Lead Program Manager, Microsoft.

This is a partial list of companies who are using our libraries:
ABB Robotics
Allstate
Applied Materials
Arcam
Astra Schedule
Babson College
Canadian Council on Learning
Canyon Associates
Caxton Associates
CECity
Constellation Energy
CreditSights
DeepOcean
Duke University
Dynamotive
Elecsoft
Engelhard Corporation
Epcor
Equipoise Software
Galileo International
GAM UK
Gammex
GlaxoSmithKline
Global Matrix
The Hartford
Infinera Corporation
Intel
JDS Uniphase
LaBranche & Co.
Learning & Skills Council
Jacobs Consultancy
Litman Gregory
Lucas Systems
Malvern Instruments
Medrio
Merck & Co.
Mintera.
Monitor Software
MorningStar
NanoString Technologies
Paletta Invent
Parametric Portfolio Associates
Prosanos
RATA Associates
RiskShield
Ramboll
Standard & Poor's
Strategic Analysis Corporation
Univ. of Alicante
Univ. of South Carolina
vielife
Xerox
US Army