SIMD-oriented Fast Mersenne Twister (SFMT)¶
- class randomgen.sfmt.SFMT(seed=None, *, mode=None)¶
Container for the SIMD-based Mersenne Twister pseudo RNG.
- Parameters:
- seed{None, int, array_like[uint32], SeedSequence}, optional
Entropy 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
, the 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.- 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”.
Notes
SFMT
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
SFMT
state vector consists of a 768 element array of 32-bit unsigned integers plus a single integer value between 0 and 382 indicating the current position within the main array. The implementation used here augments this with a 382 element array of doubles which are used to efficiently access the random numbers produced by the SFMT generator.SFMT
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
SFMT
can be used in parallel applications by calling the methodjump
which advances the state as-if \(2^{128}\) random numbers have been generated [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 randomgen.entropy import random_entropy >>> from randomgen import Generator, SFMT >>> seed = random_entropy() >>> rs = [Generator(SFMT(seed)) for _ in range(10)] # Advance each SFMT instance by i jumps >>> for i in range(10): ... rs[i].bit_generator.jump()
Compatibility Guarantee
SFMT
makes a guarantee that a fixed seed and will always produce the same random integer stream.References
[1]Mutsuo Saito and Makoto Matsumoto, “SIMD-oriented Fast Mersenne Twister: a 128-bit Pseudorandom Number Generator.” Monte Carlo and Quasi-Monte Carlo Methods 2006, Springer, pp. 607–622, 2008.
[2]Hiroshi Haramoto, Makoto Matsumoto, and Pierre L’Ecuyer, “A Fast Jump Ahead Algorithm for Linear Recurrences in a Polynomial Space”, Sequences and Their Applications - SETA, 290–298, 2008.
- Attributes:
- lockthreading.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.
- 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 |
Parallel generation¶
| Returns a new bit generator with the state jumped |
| Jumps the state as-if 2**128 random numbers have been generated. |
Extending¶
CFFI interface | |
ctypes interface |
Testing¶
| Return randoms as generated by the underlying BitGenerator |