MersenneTwister Class

Represents a pseudo-random number generator based on the Mersenne Twister MT19937 algorithm.

Definition

Namespace: Numerics.NET.Random
Assembly: Numerics.NET (in Numerics.NET.dll) Version: 10.3.0
C#
public sealed class MersenneTwister : RandomSource<BlockGenerator32<MersenneTwisterEngine>>, 
	IRandomSourceFactory<MersenneTwister>
Inheritance
Object  →  RandomSource<BlockGenerator32<MersenneTwisterEngine>>  →  MersenneTwister
Implements
IRandomSourceFactory<MersenneTwister>

Remarks

Use the MersenneTwister class to represent a pseudo-random number generator that uses the Mersenne Twister MT19937 algorithm of Takuji Nishimura and Makoto Matsumoto.

This generator has a very long period of 219937-1. This period is a Mersenne prime number (since 19937 is prime), whence its name. The algorithm is very fast and produces very good random sequences.

MersenneTwister can be used in place of the Random class to obtain pseudo-random numbers of a higher quality.

The Mersenne Twister is a linear random number generator. This makes it unsuitable for use in cryptography applications. To generate a cryptographically secure random number suitable for creating a random password, for example, use a class derived from RandomNumberGenerator such as RNGCryptoServiceProvider.

Note: This generator is provided for legacy compatibility and historical reproducibility. For new applications, consider using modern generators like Pcg64 or Xoshiro256StarStar. This implementation uses the new Layer 3 architecture with BlockGenerator32<TEngine>.

Seed Profile support:

  • Standard (default): Hiroshima University 2002 reference seeding
  • CPlusPlus: Partial C++11 std::mt19937 compatible seeding (scalar seeds only)
  • Numpy: NumPy BitGenerator-compatible seeding

Seed profiles affect only the seeding behavior. The generation algorithm remains MT19937.

Reference: M. Matsumoto and T. Nishimura. "Mersenne Twister: A 623-dimensionally equidistributed uniform pseudorandom number generator." ACM Trans. Model. Comput. Simul. 8(1), 1998.

Constructors

MersenneTwister() Constructs a new MersenneTwister class using a time-dependent default seed value.
MersenneTwister(Int32) Initializes a new instance of the MersenneTwister class using the specified integer seed value.
MersenneTwister(Int32[]) Initializes a new instance of the MersenneTwister class using the specified array of integer seeds.
MersenneTwister(RandomOptions) Initializes a new instance of the MersenneTwister class with the specified options.
MersenneTwister(ReadOnlySpan<Byte>) Initializes a new instance of the MersenneTwister class from saved state.
MersenneTwister(Int64, SeedProfile) Constructs a new MersenneTwister class using the specified seed value.
MersenneTwister(ReadOnlySpan<UInt32>, SeedProfile) Constructs a new MersenneTwister class using the specified seed array of seed values.
MersenneTwister(Int64, StreamAddress, SeedProfile) Constructs a new MersenneTwister class using the specified seed value.
MersenneTwister(ReadOnlySpan<UInt32>, StreamAddress, SeedProfile) Constructs a new MersenneTwister class using the specified seed array of seed values.

Properties

BitsPerWord Gets the number of bits per word in the generator's native output.
(Overrides RandomSource<TGenerator>.BitsPerWord)
Name Gets a human-readable name for this random number generator.
(Overrides RandomSource<TGenerator>.Name)
StateSize Gets the number of bytes required to save/load this generator's state.
(Inherited from RandomSource<TGenerator>)
Uncached Gets an uncached facade for this random number generator.
(Inherited from RandomSource<TGenerator>)

Methods

Copy Creates a copy of this random number generator with the same state.
Create Creates a new MersenneTwister instance from the specified options.
CreateStreamTree Creates a hierarchical stream tree for generating independent RNG streams.
EqualsDetermines whether the specified object is equal to the current object.
(Inherited from Object)
Fill(Span<UInt32>) Fills a span with random 32-bit unsigned integer values.
(Inherited from RandomSource<TGenerator>)
Fill(Span<Int32>, Int32) Fills a span with random 32-bit signed integers less than the specified maximum.
(Inherited from RandomSource<TGenerator>)
Fill(Span<Int64>, Int64) Fills a span with random 64-bit signed integers less than the specified maximum.
(Inherited from RandomSource<TGenerator>)
Fill(Span<Int32>, Int32, Int32) Fills a span with random 32-bit signed integers within a specified range.
(Inherited from RandomSource<TGenerator>)
Fill(Span<Int64>, Int64, Int64) Fills a span with random 64-bit signed integers within a specified range.
(Inherited from RandomSource<TGenerator>)
GetHashCodeServes as the default hash function.
(Inherited from Object)
GetState Returns the current state of the generator as a byte array.
(Inherited from RandomSource<TGenerator>)
GetTypeGets the Type of the current instance.
(Inherited from Object)
LoadState Loads the generator's state from the source span in a stable little-endian format.
(Inherited from RandomSource<TGenerator>)
Next() Returns a non-negative random integer.
(Inherited from RandomSource<TGenerator>)
Next(Int32) Returns a non-negative random integer less than the specified maximum.
(Inherited from RandomSource<TGenerator>)
Next(Int32, Int32) Returns a random integer within a specified range.
(Inherited from RandomSource<TGenerator>)
NextBytes(Byte[]) Fills the elements of a specified array of bytes with random numbers.
(Inherited from RandomSource<TGenerator>)
NextBytes(Span<Byte>) Fills the elements of a specified span of bytes with random numbers.
(Inherited from RandomSource<TGenerator>)
NextBytes(Byte[], Boolean) Fills the elements of a specified span of bytes with random numbers.
(Inherited from RandomSource<TGenerator>)
NextBytes(Span<Byte>, Boolean) Fills the elements of a specified span of bytes with random numbers.
(Inherited from RandomSource<TGenerator>)
NextDouble Returns a random floating-point number in the range [0.0, 1.0).
(Inherited from RandomSource<TGenerator>)
NextDouble32 Returns a random number between 0 and 1.
NextInt64() Returns a non-negative random 64-bit integer.
(Inherited from RandomSource<TGenerator>)
NextInt64(Int64) Returns a non-negative random 64-bit integer less than the specified maximum.
(Inherited from RandomSource<TGenerator>)
NextInt64(Int64, Int64) Returns a random 64-bit integer within a specified range.
(Inherited from RandomSource<TGenerator>)
NextSingle Returns a random single-precision floating-point number in the range [0.0, 1.0).
(Inherited from RandomSource<TGenerator>)
NextUInt32 Returns a random 32-bit unsigned integer.
(Inherited from RandomSource<TGenerator>)
NextUInt64 Returns a random 64-bit unsigned integer.
(Inherited from RandomSource<TGenerator>)
Reinitialize Reinitializes this random number generator using the specified initialization options.
(Inherited from RandomSource<TGenerator>)
SaveState Saves the generator's state to the destination span in a stable little-endian format.
(Inherited from RandomSource<TGenerator>)
ToStringReturns a string that represents the current object.
(Inherited from Object)

See Also