Table of Contents

Class MT19937

Namespace
NumSharp
Assembly
NumSharp.dll

Mersenne Twister MT19937 pseudo-random number generator. This implementation matches NumPy's MT19937 exactly, producing identical sequences for the same seed.

public sealed class MT19937 : ICloneable
Inheritance
MT19937
Implements
Inherited Members
Extension Methods

Remarks

Based on the original C implementation by Takuji Nishimura and Makoto Matsumoto. http://www.math.sci.hiroshima-u.ac.jp/~m-mat/MT/emt.html

NumPy reference: https://github.com/numpy/numpy/blob/main/numpy/random/src/mt19937/

Constructors

MT19937()

Initializes a new instance with a time-based seed.

public MT19937()

MT19937(int)

Initializes a new instance with the specified seed.

public MT19937(int seed)

Parameters

seed int

The seed value (converted to uint).

MT19937(uint)

Initializes a new instance with the specified seed.

public MT19937(uint seed)

Parameters

seed uint

The seed value.

Properties

Key

Gets the internal state array (for serialization).

public uint[] Key { get; }

Property Value

uint[]

Pos

Gets the current position in the state array.

public int Pos { get; }

Property Value

int

Methods

Clone()

Creates a deep copy of this generator.

public MT19937 Clone()

Returns

MT19937

A new MT19937 instance with identical state.

Next(int)

Returns a random integer in [0, maxValue). Uses rejection sampling for unbiased results (matches NumPy).

public int Next(int maxValue)

Parameters

maxValue int

The exclusive upper bound.

Returns

int

A random int in [0, maxValue).

Next(int, int)

Returns a random integer in [minValue, maxValue). Uses rejection sampling for unbiased results.

public int Next(int minValue, int maxValue)

Parameters

minValue int

The inclusive lower bound.

maxValue int

The exclusive upper bound.

Returns

int

A random int in [minValue, maxValue).

NextBytes(byte[])

Fills a byte array with random bytes.

public void NextBytes(byte[] buffer)

Parameters

buffer byte[]

The array to fill.

NextDouble()

Returns a random double in [0, 1) with 53-bit precision. This matches NumPy's random_standard_uniform exactly.

public double NextDouble()

Returns

double

A random double in [0, 1).

NextInt()

Returns a random signed 32-bit integer.

public int NextInt()

Returns

int

A random int in [0, Int32.MaxValue].

NextLong()

Returns a random long in [0, Int64.MaxValue].

public long NextLong()

Returns

long

A random long.

NextLong(long)

Returns a random long in [0, maxValue). Uses rejection sampling for unbiased results.

public long NextLong(long maxValue)

Parameters

maxValue long

The exclusive upper bound.

Returns

long

A random long in [0, maxValue).

NextLong(long, long)

Returns a random long in [minValue, maxValue). Uses rejection sampling for unbiased results.

public long NextLong(long minValue, long maxValue)

Parameters

minValue long

The inclusive lower bound.

maxValue long

The exclusive upper bound.

Returns

long

A random long in [minValue, maxValue).

NextLongNumPy(long, long)

Returns a random long in [low, high) using NumPy's algorithm. NumPy uses: floor(nextDouble() * range) + low for small ranges. This matches NumPy's legacy RandomState.randint() exactly.

public long NextLongNumPy(long low, long high)

Parameters

low long

The inclusive lower bound.

high long

The exclusive upper bound.

Returns

long

A random long in [low, high).

NextUInt32()

Returns a random unsigned 32-bit integer.

public uint NextUInt32()

Returns

uint

A random uint in [0, 2^32).

Seed(uint)

Seeds the generator with a single integer. This matches NumPy's seeding algorithm exactly.

public void Seed(uint seed)

Parameters

seed uint

The seed value.

SeedByArray(uint[])

Seeds the generator with an array of integers. This matches NumPy's init_by_array function exactly.

public void SeedByArray(uint[] initKey)

Parameters

initKey uint[]

Array of seed values.

SetState(uint[], int)

Sets the internal state from serialized state data.

public void SetState(uint[] key, int pos)

Parameters

key uint[]

The state array (must be length 624).

pos int

The position in the state array (0-624).