numpy.random.PCG64.advance#

method

random.PCG64.advance(delta)#

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

Parameters:
deltainteger, positive

Number of draws to advance the RNG. Must be less than the size state variable in the underlying RNG.

Returns:
selfPCG64

RNG advanced delta steps

Notes

Avoid a delta that is, or is close to, a multiple of a large power of two. The state is a 128-bit linear congruential generator, and advancing it by a multiple of 2**N leaves the lowest N bits of the state unchanged. The original and advanced streams then share those bits at every later draw, and their outputs are correlated even though delta is large. An odd delta with well-mixed bits avoids this. A simple choice is the odd integer nearest to the required distance divided by the golden ratio, which is how jumped chooses its step. See Upgrading PCG64 with PCG64DXSM for more on this weakness of the underlying generator.

Advancing an RNG updates the underlying RNG state as-if a given number of calls to the underlying RNG have been made. In general there is not a one-to-one relationship between the number output random values from a particular distribution and the number of draws from the core RNG. This occurs for two reasons:

  • The random values are simulated using a rejection-based method and so, on average, more than one value from the underlying RNG is required to generate a single draw.

  • The number of bits required to generate a simulated value differs from the number of bits generated by the underlying RNG. For example, two 16-bit integer values can be simulated from a single draw of a 32-bit RNG.

Advancing the RNG state resets any pre-computed random numbers. This is required to ensure exact reproducibility.

Examples

Advance a generator by about 2**48 steps, using the odd integer nearest to 2**48 / phi, where phi is the golden ratio. The step is about 62% of 2**48. Integer arithmetic keeps the calculation exact at any scale, including 2**128.

>>> import math
>>> from numpy.random import Generator, PCG64
>>> bg = PCG64(20261114)
>>> scale = 2**48
>>> delta = ((math.isqrt(5 * scale**2) - scale) // 2) | 1
>>> delta
173961102589771

advance modifies bg in place and returns it, so it can be passed straight to Generator.

>>> rg = Generator(bg.advance(delta))