Extreme Optimization™: Complexity made simple.

Math and Statistics
Libraries 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
    • Data Analysis Library User's Guide
    • Statistics Library User's Guide
    • Reference
  • Resources
    • Downloads
    • QuickStart Samples
    • Sample Applications
    • Frequently Asked Questions
    • Technical Support
  • Order
  • Company
    • About us
    • Testimonials
    • Customers
    • Press Releases
    • Careers
    • Partners
    • Contact us
Introduction
Deployment Guide
Nuget packages
Configuration
Using Parallelism
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 Data Analysis Library User's GuideData Analysis Library User's Guide
Expand Statistics Library User's GuideStatistics Library User's Guide
Expand Data Access Library User's GuideData Access Library User's Guide
Expand ReferenceReference

Skip Navigation LinksHome»Documentation»Mathematics Library User's Guide»Random Numbers»Random Enumerators and Shufflers

Random Enumerators and Shufflers

Extreme Optimization Numerical Libraries for .NET Professional

The Extreme Optimization Numerical Libraries 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.Mathematics.Random namespace.

Shuffling lists

The Shuffler class lets you shuffle the contents of any class that implements the ListT interface. This includes arrays, vectors and many other objects.

The class has only one static (Shared in Visual Basic) method, Shuffle, which is overloaded. The first overload takes two arguments: an ListT, and a Random that is used to generate the random sequence. This can be an instance of the Random itself, or one of the random number generators from the Extreme.Mathematics.Random namespace. The second variant only takes the list to shuffle, and uses a default random number generator.

C#
VB
C++
F#
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]);
Dim numbers = {1, 2, 3, 4, 5, 6}
Shuffler.Shuffle(numbers, New MersenneTwister())
For i = 0 To numbers.Length - 1
    Console.WriteLine(numbers(i))
Next

No code example is currently available or this language may not be supported.

let numbers = [| 1; 2; 3; 4; 5; 6 |]
Shuffler.Shuffle(numbers, MersenneTwister())
for number in numbers do
    printf "%d " number

These methods overwrite the contents of the list with the shuffled data.

Enumerating Sequences in Random Order

The RandomizedCollectionT class is a generic wrapper for sequences that enumerates the elements of the sequence in random order. The following example enumerates the elements of an array in random order:

C#
VB
C++
F#
Copy
int[] numbers = new int[] { 1, 2, 3, 4, 5, 6 };
var randomized = new RandomizedCollection<int>(numbers);
foreach (int number in randomized)
    Console.WriteLine(number);
Dim numbers = {1, 2, 3, 4, 5, 6}
Dim randomized = New RandomizedCollection(Of Integer)(numbers)
For Each number In randomized
    Console.WriteLine(number)
Next

No code example is currently available or this language may not be supported.

let numbers = [| 1; 2; 3; 4; 5; 6 |]
let randomized = RandomizedCollection<int>(numbers)
for number in randomized do
    printf "%d " number

RandomizedCollectionT works most efficiently with collections that implement the IListT interface, but can work with IEnumerableT sequences as well. In the latter case, all the values in the sequence are enumerated before the first random element is returned.

Copyright (c) 2004-2023 ExoAnalytics Inc.

Send comments on this topic to support@extremeoptimization.com

Copyright © 2004-2023, 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.