Permuted Congruential Generator (32-bit, PCG32)

class randomgen.pcg32.PCG32(seed=None, inc=0, *, mode=None)

Container for the PCG-32 pseudo-random number generator.

Parameters:
seed{None, int, SeedSequence}, optional

Random seed initializing the pseudo-random number generator. Can be an integer in [0, 2**64], a SeedSequence instance or None (the default). If seed is None, then PCG32 will try to read data from /dev/urandom (or the Windows analog) if available. If unavailable, a 64-bit hash of the time and process ID is used.

inc{None, int}, optional

The increment in the LCG. Can be an integer in [0, 2**64] or None. The default is 0. If inc is None, then it is initialized using entropy.

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

PCG-32 is a 64-bit implementation of O’Neill’s permuted congruential generator ([1], [2]). PCG-32 has a period of \(2^{64}\) and supports advancing an arbitrary number of steps.

PCG32 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.

Supports the method advance to advance the RNG an arbitrary number of steps. The state of the PCG-32 PRNG is represented by 2 64-bit unsigned integers.

See PCG64 for a similar implementation with a smaller period.

State and Seeding

The PCG32 state vector consists of 2 unsigned 64-bit values. PCG32 is seeded using a single 64-bit unsigned integer.

Parallel Features

PCG32 can be used in parallel applications using advance with a different value in each instance to produce non-overlapping sequences.

>>> from numpy.random import Generator
>>> from randomgen import PCG32
>>> rg = [Generator(PCG32(1234, i + 1)) for i in range(10)]
>>> for i in range(10):
...     rg[i].bit_generator.advance(i * 2**32)

Compatibility Guarantee

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

References

[1]

“PCG, A Family of Better Random Number Generators”, https://www.pcg-random.org/

[2]

O’Neill, Melissa E. “PCG: A Family of Simple Fast Space-Efficient Statistically Good Algorithms for Random Number Generation”

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([seed, inc])

Seed the generator

state

Get or set the PRNG state

Parallel generation

advance(delta)

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

jump([iter])

Jump the state a fixed increment

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