Click or drag to resize

Random Generator

RandomGenerator implements computational algorithms that produce apparently random results, which are in fact completely determined by a shorter initial value, known as a seed.

This algorithms that can automatically create long runs of numbers with good random properties. One of the most common methods is the linear congruential generator, which uses the recurrence

RGReccurence

to generate numbers. The fixed integers a, b and m are called the multiplier, increment, and modulus, respectively. The maximum number of numbers the formula can produce is the modulus, m.

There are 10 available methods of random number generation which are implemented as RandomGeneratorType:

  • MCG31 - a 31-bit multiplicative congruential generator.

  • R250 - a generalized feedback shift register generator.

  • MRG32K3A - a combined multiple recursive generator with two components of order 3.

  • MCG59 - a 59-bit multiplicative congruential generator.

  • WH - a set of 273 Wichmann-Hill combined multiplicative congruential generators.

  • SOBOL - 32-bit Gray code-based generator producing low-discrepancy sequences for dimensions 1 ≤ s ≤ 40; user-defined dimensions are also available.

  • NIEDERR - 32-bit Gray code-based generator producing low-discrepancy sequences for dimensions 1 ≤ s ≤ 318; user-defined dimensions are also available.

  • MT19937 - a Mersenne Twister pseudorandom number generator.

  • MT2203 - a set of 1024 Mersenne Twister pseudorandom number generators.

  • SFMT19937 - a SIMD-oriented Fast Mersenne Twister pseudorandom number generator.

This topic contains the following sections:

Constructors

Constructor

Description

Performance

set seed and generator

Initializes a new instance of the RandomGenerator, using the specified seed value and random generator type.

methodRandomGenerator(Int32, RandomGeneratorType)

set seed

Initializes a new instance of the RandomGenerator, using the specified seed value. By default the SIMD-oriented fast Mersenne-Twister pseudorandom number generator will be used.

methodRandomGenerator(Int32)

set generator

Initializes a new instance of the RandomGenerator, using the specified generator type and a computer's real time clock as the seed.

methodRandomGenerator(Int32)

default seed and generator

Initializes a new instance of the RandomGenerator, using a computer's real time clock as the seed. By default the SIMD-oriented fast Mersenne-Twister pseudorandom number generator will be used.

methodRandomGenerator

Methods

RandomGenerator class provides methods to generate an integer, double, series of integers, series of doubles and vector of doubles. The following settings are available for each type of generated values:

  • default range of values: [0, Int32.MaxValue) for integers and [0.0, 1.0) for doubles

  • set max value to generate a value within the range [0, max)

  • set min and max value to generate a value within the range [min, max)

Method

Description

Performance

generate integer

Returns an integer random number.

methodNext

methodNext(Int32)

methodNext(Int32, Int32)

generate integer series

Generates a series of integer random numbers.

methodNextSeries(Int32)

methodNextSeries(Int32, Int32)

methodNextSeries(Int32, Int32, Int32)

generate double

Returns a double random number.

methodNextDouble

methodNextDouble(Double)

methodNextDouble(Double, Double)

generate double series

Generates a series of double random numbers.

methodNextSeries(Double)

methodNextSeries(Double, Double)

methodNextSeries(Double, Double, Double)

generate vector of doubles

Generates a vector with double random elements.

In place:

methodNextSeries(Vector)

methodNextSeries(Vector, Double)

methodNextSeries(Vector, Double, Double)

Returning result:

methodNextSeries(Int32)

methodNextSeries(Int32, Double)

methodNextSeries(Int32, Double, Double)

Code Sample

The example of RandomGenerator class usage:

C#
  1using System;
  2using FinMath.LinearAlgebra;
  3using FinMath.Statistics;
  4
  5namespace FinMath.Samples
  6{
  7    class RandomGeneratorSample
  8    {
  9        static void Main(string[] args)
 10        {
 11-            #region Construting generator.
 12 
 13             // Default constructor.
 14             RandomGenerator randomGenerator1 = new RandomGenerator();
 15 
 16             // Specify type. A 59-bit multiplicative congruential generator used.
 17             RandomGenerator randomGenerator2 = new RandomGenerator(RandomGeneratorType.MCG59);
 18 
 19             // Specify seed.
 20             RandomGenerator randomGenerator3 = new RandomGenerator(12345);
 21 
 22             // Specify type and seed. 
 23             RandomGenerator randomGenerator4 = new RandomGenerator(12345, RandomGeneratorType.MCG59);
 24 
 25             // Use default random generator.
 26             RandomGenerator randomGenerator5 = FinMath.FMControl.DefaultGenerator.Value;
 27 
 28             #endregion
 29
 30-            #region Generating next value.
 31 
 32             // Generate reals from [0. 1], [0, 200], [100, 200].
 33             Console.Write(randomGenerator1.NextDouble().ToString("0.000") + "\t");
 34             Console.Write(randomGenerator1.NextDouble(200.0).ToString("0.000") + "\t");
 35             Console.WriteLine(randomGenerator1.NextDouble(100.0, 200.0).ToString("0.000"));
 36 
 37             // Generate integers from 0..Int32.MaxValue, 0..200, 100..200.
 38             Console.Write(randomGenerator1.Next() + "\t");
 39             Console.Write(randomGenerator1.Next(200) + "\t");
 40             Console.WriteLine(randomGenerator1.Next(100, 200));
 41 
 42 
 43             // Generate reals from [0. 1], [0, 200], [100, 200].
 44             Console.Write(randomGenerator2.NextDouble().ToString("0.000") + "\t");
 45             Console.Write(randomGenerator2.NextDouble(200.0).ToString("0.000") + "\t");
 46             Console.WriteLine(randomGenerator2.NextDouble(100.0, 200.0).ToString("0.000"));
 47 
 48             // Generate integers from 0..Int32.MaxValue, 0..200, 100..200.
 49             Console.Write(randomGenerator2.Next() + "\t");
 50             Console.Write(randomGenerator2.Next(200) + "\t");
 51             Console.WriteLine(randomGenerator2.Next(100, 200));
 52 
 53 
 54             // Generate reals from [0. 1], [0, 200], [100, 200].
 55             Console.Write(randomGenerator3.NextDouble().ToString("0.000") + "\t");
 56             Console.Write(randomGenerator3.NextDouble(200.0).ToString("0.000") + "\t");
 57             Console.WriteLine(randomGenerator3.NextDouble(100.0, 200.0).ToString("0.000"));
 58 
 59             // Generate integers from 0..Int32.MaxValue, 0..200, 100..200.
 60             Console.Write(randomGenerator3.Next() + "\t");
 61             Console.Write(randomGenerator3.Next(200) + "\t");
 62             Console.WriteLine(randomGenerator3.Next(100, 200));
 63 
 64 
 65             // Generate reals from [0. 1], [0, 200], [100, 200].
 66             Console.Write(randomGenerator4.NextDouble().ToString("0.000") + "\t");
 67             Console.Write(randomGenerator4.NextDouble(200.0).ToString("0.000") + "\t");
 68             Console.WriteLine(randomGenerator4.NextDouble(100.0, 200.0).ToString("0.000"));
 69 
 70             // Generate integers from 0..Int32.MaxValue, 0..200, 100..200.
 71             Console.Write(randomGenerator4.Next() + "\t");
 72             Console.Write(randomGenerator4.Next(200) + "\t");
 73             Console.WriteLine(randomGenerator4.Next(100, 200));
 74 
 75 
 76             // Generate reals from [0. 1], [0, 200], [100, 200].
 77             Console.Write(randomGenerator5.NextDouble().ToString("0.000") + "\t");
 78             Console.Write(randomGenerator5.NextDouble(200.0).ToString("0.000") + "\t");
 79             Console.WriteLine(randomGenerator5.NextDouble(100.0, 200.0).ToString("0.000"));
 80 
 81             // Generate integers from 0..Int32.MaxValue, 0..200, 100..200.
 82             Console.Write(randomGenerator5.Next() + "\t");
 83             Console.Write(randomGenerator5.Next(200) + "\t");
 84             Console.WriteLine(randomGenerator5.Next(100, 200));
 85 
 86             Console.WriteLine();
 87 
 88             #endregion
 89
 90-            #region Generating series of values.
 91 
 92             // Generate series of reals from [0, 200].
 93             Vector series1 = randomGenerator1.NextSeries(4, 200.0);
 94 
 95             Console.WriteLine(series1);
 96 
 97             // Generate series of reals from [100, 200].
 98             Double[] series2 = new Double[8];
 99             randomGenerator1.NextSeries(series2, 100.0, 200.0);
100 
101             for (Int32 i = 0; i < series2.Length; i++)
102             {
103                 Console.Write(series2[i].ToString("0.000"));
104                 if (i == series2.Length - 1)
105                     Console.WriteLine();
106                 else
107                     Console.Write(", ");
108             }
109 
110             // Generate series of reals from [0, 1].
111             Vector series3 = new Vector(4);
112             randomGenerator1.NextSeries(series3);
113 
114             Console.WriteLine(series3);
115 
116             // Generate series of integers from 0..200.
117             Int32[] series4 = new Int32[8];
118             randomGenerator1.NextSeries(series4, 200);
119 
120             for (Int32 i = 0; i < series4.Length; i++)
121             {
122                 Console.Write(series4[i]);
123                 if (i == series4.Length - 1)
124                     Console.WriteLine();
125                 else
126                     Console.Write(", ");
127             }
128 
129             Console.WriteLine();
130 
131             #endregion
132
133-            #region Notes.
134 
135             // Generators with same seed give same numbers.
136 
137             RandomGenerator randomGenerator6 = new RandomGenerator(12345);
138             Console.WriteLine(randomGenerator6.NextSeries(4, 100.0, 200.0));
139 
140             RandomGenerator randomGenerator7 = new RandomGenerator(12345);
141             Console.WriteLine(randomGenerator7.NextSeries(4, 100.0, 200.0));
142 
143             Console.WriteLine();
144 
145             // Generators created simultaneously with no seed specified 
146             // almost surely have the same seed.
147 
148             RandomGenerator randomGenerator8 = new RandomGenerator();
149             Console.WriteLine(randomGenerator8.NextSeries(4, 100.0, 200.0));
150 
151             RandomGenerator randomGenerator9 = new RandomGenerator();
152             Console.WriteLine(randomGenerator9.NextSeries(4, 100.0, 200.0));
153 
154             #endregion
155        }
156    }
157}