MersenneTwisterEngine Structure

Represents a Mersenne Twister (MT19937) random number generator engine with 32-bit output.

Definition

Namespace: Numerics.NET.Random.Engines
Assembly: Numerics.NET (in Numerics.NET.dll) Version: 10.3.0
C#
public struct MersenneTwisterEngine : IBlockEngine<uint>, 
	IRandomEngine, IStateful
Inheritance
Object  →  ValueType  →  MersenneTwisterEngine
Implements
IBlockEngine<UInt32>, IRandomEngine, IStateful

Remarks

The Mersenne Twister (MT19937) engine is a widely-used pseudo-random number generator developed by Takuji Nishimura and Makoto Matsumoto in 1997. It features a 19937-bit state with an exceptionally long period of 219937 - 1 (a Mersenne prime).

Algorithm Details:

  • State size: 19968 bits (624 × 32-bit words + index)
  • Period: 219937 - 1
  • Equidistribution: 623-dimensionally equidistributed
  • Performance: Good speed with large state array

Historical Context:

MT19937 was the gold standard for non-cryptographic random number generation for many years and is still widely used in scientific computing. However, it has some limitations: large state size (2.5 KB), slow recovery from zero-excess initial states, and failure of certain modern statistical tests.

Modern Alternatives:

For new applications, consider modern generators like Xoshiro256StarStarEngine or Pcg64Engine which offer similar or better statistical quality with smaller state, faster performance, and additional features like jumping.

Note: This generator is provided for legacy compatibility and historical reproducibility. It does not support jumping or fast-forward operations.

Reference: Takuji Nishimura and Makoto Matsumoto. "Mersenne Twister: A 623-dimensionally equidistributed uniform pseudo-random number generator." ACM Trans. Model. Comput. Simul. 1998.

See also: http://www.math.sci.hiroshima-u.ac.jp/~m-mat/MT/emt.html

Example

C#
var engine = new MersenneTwisterEngine();
engine.Seed(SeedSequences.SplitMix64(5489));

uint value = engine.Next();

uint[] values = new uint[100];
engine.Fill(values);

// Save and restore state
byte[] state = new byte[engine.StateSize];
engine.SaveState(state);
engine.LoadState(state);

Constructors

MersenneTwisterEngine Initializes a new Mersenne Twister engine.

Properties

BitsPerWord Gets the native word size in bits.
BlockLength Gets the number of native words addressable in each block.
Name Gets a human-readable name for this engine.
StateSize Gets the number of bytes required to save/load this engine's state.

Methods

AdvanceBlock Advances the engine state to the next block (next entropy window).
EqualsIndicates whether this instance and a specified object are equal.
(Inherited from ValueType)
Fill Fills the destination span with values from a contiguous range of indices within the current block.
GetHashCodeReturns the hash code for this instance.
(Inherited from ValueType)
GetTypeGets the Type of the current instance.
(Inherited from Object)
LoadState Loads the engine's state from the source span in a stable little-endian format.
Output Returns the native word at the specified index within the current block.
SaveState Saves the engine's state to the destination span in a stable little-endian format.
Seed(ReadOnlySpan<UInt32>) Initializes the generator with an array of keys (init_by_array).
Seed(SeedSequence) Initializes the engine state from a seed sequence.
Seed(UInt32) Initializes the generator with a single seed (init_genrand).
ToStringReturns the fully qualified type name of this instance.
(Inherited from ValueType)

See Also