[Numpy-discussion] skip samples in random number generator

Robert Kern robert.kern at gmail.com
Thu Oct 2 11:52:11 EDT 2014

On Thu, Oct 2, 2014 at 4:42 PM, Brad Buran <bburan at alum.mit.edu> wrote:
> Given the following:
> from numpy import random
> rs = random.RandomState(seed=1)
> # skip the first X billion samples
> x = rs.uniform(0, 10)
> How do I accomplish "skip the first X billion samples" (e.g. 7.2
> billion)?  I see that there's a numpy.random.RandomState.set_state
> which accepts (among other parameters) a value called "pos".  This
> sounds promising, but the other parameters I'm not sure how to compute
> (e.g. the 1D array of 624 unsigned integers, etc.).  I need to be able
> to skip ahead in the sequence to reproduce some signals that were
> generated for experiments.  I could certainly consume and discard the
> first X billion samples; however, that seems to be computationally
> inefficient.

Unfortunately, it requires some significant number-theoretical
precomputation for any given N number of steps that you want to skip.


It's also unreliable for your purpose. You don't know how many random
integers were actually pulled from the core PRNG if you ever used any
of the nonuniform distributions (they usually will consume a variable
number of uniform pseudorandom numbers to give you a single nonuniform

Instead, you should just pickle the RandomState object just before you
start using it for anything that you want to reproduce. The unpickled
RandomState will reproduce the same numbers the first one did.

Robert Kern

More information about the NumPy-Discussion mailing list