Mersenne Twister Class
Definition
Assembly: Numerics.NET (in Numerics.NET.dll) Version: 10.3.0
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
| Mersenne | Constructs a new MersenneTwister class using a time-dependent default seed value. |
| Mersenne | Initializes a new instance of the MersenneTwister class using the specified integer seed value. |
| Mersenne | Initializes a new instance of the MersenneTwister class using the specified array of integer seeds. |
| Mersenne | Initializes a new instance of the MersenneTwister class with the specified options. |
| Mersenne | Initializes a new instance of the MersenneTwister class from saved state. |
| Mersenne | Constructs a new MersenneTwister class using the specified seed value. |
| Mersenne | Constructs a new MersenneTwister class using the specified seed array of seed values. |
| Mersenne | Constructs a new MersenneTwister class using the specified seed value. |
| Mersenne | Constructs a new MersenneTwister class using the specified seed array of seed values. |
Properties
| Bits |
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) |
| State |
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. |
| Create | Creates a hierarchical stream tree for generating independent RNG streams. |
| Equals | Determines whether the specified object is equal to the current object. (Inherited from Object) |
| Fill( |
Fills a span with random 32-bit unsigned integer values.
(Inherited from RandomSource<TGenerator>) |
| Fill( |
Fills a span with random 32-bit signed integers less than the specified maximum.
(Inherited from RandomSource<TGenerator>) |
| Fill( |
Fills a span with random 64-bit signed integers less than the specified maximum.
(Inherited from RandomSource<TGenerator>) |
| Fill( |
Fills a span with random 32-bit signed integers within a specified range.
(Inherited from RandomSource<TGenerator>) |
| Fill( |
Fills a span with random 64-bit signed integers within a specified range.
(Inherited from RandomSource<TGenerator>) |
| Get | Serves as the default hash function. (Inherited from Object) |
| Get |
Returns the current state of the generator as a byte array.
(Inherited from RandomSource<TGenerator>) |
| Get | Gets the Type of the current instance. (Inherited from Object) |
| Load |
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( |
Returns a non-negative random integer less than the specified maximum.
(Inherited from RandomSource<TGenerator>) |
| Next( |
Returns a random integer within a specified range.
(Inherited from RandomSource<TGenerator>) |
| Next |
Fills the elements of a specified array of bytes with random numbers.
(Inherited from RandomSource<TGenerator>) |
| Next |
Fills the elements of a specified span of bytes with random numbers.
(Inherited from RandomSource<TGenerator>) |
| Next |
Fills the elements of a specified span of bytes with random numbers.
(Inherited from RandomSource<TGenerator>) |
| Next |
Fills the elements of a specified span of bytes with random numbers.
(Inherited from RandomSource<TGenerator>) |
| Next |
Returns a random floating-point number in the range [0.0, 1.0).
(Inherited from RandomSource<TGenerator>) |
| Next | Returns a random number between 0 and 1. |
| Next |
Returns a non-negative random 64-bit integer.
(Inherited from RandomSource<TGenerator>) |
| Next |
Returns a non-negative random 64-bit integer less than the specified maximum.
(Inherited from RandomSource<TGenerator>) |
| Next |
Returns a random 64-bit integer within a specified range.
(Inherited from RandomSource<TGenerator>) |
| Next |
Returns a random single-precision floating-point number in the range [0.0, 1.0).
(Inherited from RandomSource<TGenerator>) |
| Next |
Returns a random 32-bit unsigned integer.
(Inherited from RandomSource<TGenerator>) |
| Next |
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>) |
| Save |
Saves the generator's state to the destination span in a stable little-endian format.
(Inherited from RandomSource<TGenerator>) |
| ToString | Returns a string that represents the current object. (Inherited from Object) |