Mersenne Twister Engine Structure
Definition
Assembly: Numerics.NET (in Numerics.NET.dll) Version: 10.3.0
public struct MersenneTwisterEngine : IBlockEngine<uint>,
IRandomEngine, IStateful- 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
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
| Mersenne | Initializes a new Mersenne Twister engine. |
Properties
| Bits | Gets the native word size in bits. |
| Block | Gets the number of native words addressable in each block. |
| Name | Gets a human-readable name for this engine. |
| State | Gets the number of bytes required to save/load this engine's state. |
Methods
| Advance | Advances the engine state to the next block (next entropy window). |
| Equals | Indicates 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. |
| Get | Returns the hash code for this instance. (Inherited from ValueType) |
| Get | Gets the Type of the current instance. (Inherited from Object) |
| Load | 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. |
| Save | Saves the engine's state to the destination span in a stable little-endian format. |
| Seed( | Initializes the generator with an array of keys (init_by_array). |
| Seed( | Initializes the engine state from a seed sequence. |
| Seed( | Initializes the generator with a single seed (init_genrand). |
| ToString | Returns the fully qualified type name of this instance. (Inherited from ValueType) |