64-bit Mersenne Twister

class randomgen.mt64.MT64(seed=None, *, mode=None)

Container for the 64-bit Mersenne Twister pseudo-random number generator

seed{None, int, array_like[uint64], SeedSequence}, optional

Random seed used to initialize the pseudo-random number generator. Can be any integer between 0 and 2**64 - 1 inclusive, an array (or other sequence) of unsigned 64-bit integers, a SeedSequence instance or None (the default). If seed is None, then 312 64-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.

mode{None, “sequence”, “legacy”}, optional

The seeding mode to use. “legacy” uses the legacy SplitMix64-based initialization. “sequence” uses a SeedSequence to transforms the seed into an initial state. None defaults to “sequence”.


MT64 provides a capsule containing function pointers that produce doubles, and unsigned 32 and 64- bit integers ([1], [2]). These are not directly consumable in Python and must be consumed by a Generator 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 MT64 state vector consists of a 312-element array of 64-bit unsigned integers plus a single integer value between 0 and 312 that indexes the current position within the main array.

MT64 is seeded using either a single 64-bit unsigned integer or a vector of 64-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 64-bit value for the seed can only initialize a small range of the possible initial state values.

Compatibility Guarantee

MT64 makes a guarantee that a fixed seed and will always produce the same random integer stream.



Matsumoto, M.; Nishimura, T. (1998). “Mersenne twister: a 623-dimensionally equidistributed uniform pseudo-random number generator”. ACM Transactions on Modeling and Computer Simulation. 8 (1): 3–30.


Nishimura, T. “Tables of 64-bit Mersenne Twisters” ACM Transactions on Modeling and Computer Simulation 10. (2000) 348-357.


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.

seed_seq{None, SeedSequence}

The SeedSequence instance used to initialize the generator if mode is “sequence” or is seed is a SeedSequence. None if mode is “legacy”.

Seeding and State


Seed the generator


Get or set the PRNG state



CFFI interface


ctypes interface


random_raw([size, output])

Return randoms as generated by the underlying BitGenerator