Mersenne Twister (MT19937)¶
-
class randomgen.mt19937.MT19937(seed=
None
, *, numpy_seed=False
, mode='sequence'
)¶ Container for the Mersenne Twister pseudo-random number generator.
- Parameters:¶
- seed=
None
¶ Random seed used to initialize the pseudo-random number generator. Can be any integer between 0 and 2**32 - 1 inclusive, an array (or other sequence) of unsigned 32-bit integers, a SeedSequence instance or
None
(the default). If seed isNone
, then 624 32-bit unsigned integers are read from/dev/urandom
(or the Windows analog) if available. If unavailable, a hash of the time and process ID is used.- numpy_seed=
False
¶ Set to True to use the same seeding mechanism as NumPy and so matches NumPy exactly.
- mode=
'sequence'
¶ “sequence” uses a SeedSequence to transforms the seed into an initial state. None defaults to “sequence”. “numpy” uses the same seeding mechanism as NumPy and so matches NumPy exactly.
- seed=
- lock¶
Lock instance that is shared so that the same bit git generator can be used in multiple Generators without corrupting the state. Code that generates values from a bit generator should hold the bit generator’s lock.
- Type:¶
- seed_seq¶
The SeedSequence instance used to initialize the generator if mode is “sequence” or is seed is a SeedSequence.
- Type:¶
{None, SeedSequence}
Notes
MT19937
provides a capsule containing function pointers that produce doubles, and unsigned 32 and 64- bit integers [1]. These are not directly consumable in Python and must be consumed by aGenerator
or similar object that supports low-level access.The Python stdlib module “random” also contains a Mersenne Twister pseudo-random number generator.
State and Seeding
The
MT19937
state vector consists of a 768-element array of 32-bit unsigned integers plus a single integer value between 0 and 768 that indexes the current position within the main array.MT19937
is seeded using either a single 32-bit unsigned integer or a vector of 32-bit unsigned integers. In either case, the input seed is used as an input (or inputs) for a hashing function, and the output of the hashing function is used as the initial state. Using a single 32-bit value for the seed can only initialize a small range of the possible initial state values.Parallel Features
MT19937
can be used in parallel applications by calling the methodjump
which advances the state as-if \(2^{128}\) random numbers have been generated ([1], [2]). This allows the original sequence to be split so that distinct segments can be used in each worker process. All generators should be initialized with the same seed to ensure that the segments come from the same sequence.>>> from numpy.random import Generator >>> from randomgen.entropy import random_entropy >>> from randomgen import MT19937 >>> seed = random_entropy() >>> rs = [Generator(MT19937(seed)) for _ in range(10)] # Advance each MT19937 instance by i jumps >>> for i in range(10): ... rs[i].bit_generator.jump(i)
Compatibility Guarantee
MT19937
makes a guarantee that a fixed seed and will always produce the same random integer stream.References
Seeding and State¶
|
Seed the generator |
Get or set the PRNG state |
Parallel generation¶
|
Jumps the state as-if 2**128 random numbers have been generated. |
|
Returns a new bit generator with the state jumped |
Extending¶
CFFI interface |
|
ctypes interface |
Testing¶
|
Return randoms as generated by the underlying BitGenerator |