Extreme Optimization™: Complexity made simple.

Numerical Components
for .NET

  • Home
  • •
  • Features
    • Math Library
    • Vector and Matrix Library
    • Statistics Library
    • Performance
    • Usability
  • •
  • Documentation
    • Introduction
    • Math Library User's Guide
    • Vector and Matrix Library User's Guide
    • Statistics Library User's Guide
    • Reference
  • •
  • Support
    • Frequently Asked Questions
    • QuickStart Samples
    • Sample Applications
    • Downloads
  • •
  • Blog
  • •
  • Company
    • About us
    • Testimonials
    • Customers
    • Press Releases
    • Careers
    • Contact us
Introduction
Expand Mathematics Library User's GuideMathematics Library User's Guide
Expand Vector and Matrix Library User's GuideVector and Matrix Library User's Guide
Expand Statistics Library User's GuideStatistics Library User's Guide
Expand ReferenceReference
  • Home
  • Documentation
  • Statistics Library User's Guide
  • Random Numbers
  • Random Enumerators and Shufflers
Collapse imageExpand ImageCopy imageCopyHover image
       




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:

C# Copy imageCopy
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 imageCopy
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 imageCopy
Shuffler.Shuffle(numbers);
for(int i = 0; i < numbers.Length; i++)
    Console.WriteLine(numbers[i]);
Visual Basic Copy imageCopy
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 imageCopy
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 imageCopy
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.

Send comments on this topic to support@extremeoptimization.com

Copyright © 2003-2010, 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 Optimized for Visual Studio logo
are registered trademarks of Microsoft Corporation.