MWC128X/32

class randomgen.mwcx.MWC128X32(seed=None)

Container for the MWC128X/32 pseudo-random number generator.

Parameters:
seed=None

A seed to initialize the BitGenerator. If None, then fresh, unpredictable entropy will be pulled from the OS. If an int or array_like[ints] is passed, then it will be passed to SeedSequence to derive the initial BitGenerator state. One may also pass in a SeedSequence instance.

lock

Lock instance that is shared so that the same bit 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:

threading.Lock

seed_seq

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

Type:

{None, SeedSequence}

Notes

MWC128X/32 is a multiply-with-carry generator with 32-bit digits and output, lag three, and period approximately \(2^{127}\), whose output is scrambled by X. It is the suggested generator of the family for 32-bit outputs. Since it simulates a multiplicative congruential generator with prime modulus, it can be advanced by an arbitrary number of steps, also backward, in logarithmic time. See [1] for details.

MWC128X32 provides a capsule containing function pointers that produce doubles, and unsigned 32 and 64- bit integers. These are not directly consumable in Python and must be consumed by a Generator or similar object that supports low-level access.

State and Seeding

The MWC128X32 state consists of three 32-bit digits and a 32-bit carry. It is seeded using a SeedSequence, which fills the whole state; the carry is set to one if the resulting state is not valid.

Parallel Features

MWC128X32 can be used in parallel applications by calling the method jumped which advances the state as-if \(2^{64}\) random numbers have been generated, or advance, which advances the state by an arbitrary number of steps. This allows the original sequence to be split so that distinct segments can be used in each worker process. All generators should be chained to ensure that the segments come from the same sequence.

>>> from numpy.random import Generator
>>> from randomgen import MWC128X32
>>> bit_gen = MWC128X32(1234)
>>> rg = []
>>> for _ in range(10):
...    rg.append(Generator(bit_gen))
...    # Chain the BitGenerators
...    bit_gen = bit_gen.jumped()

Compatibility Guarantee

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

Examples

>>> from numpy.random import Generator
>>> from randomgen import MWC128X32
>>> rg = Generator(MWC128X32(1234))
>>> rg.standard_normal()
0.123  # random

References

Seeding and State

seed([seed])

Seed the generator

state

Get or set the PRNG state

Parallel generation

advance(delta)

Advance the underlying RNG as-if delta draws have occurred.

jumped([iter])

Returns a new bit generator with the state jumped

Extending

cffi

CFFI interface

ctypes

ctypes interface

Testing

random_raw([size, output])

Return randoms as generated by the underlying BitGenerator