简体   繁体   中英

Discrepancy of the state of `numpy.random` disappears

There are two python runs of the same project with different settings, but with the same random seeds.

The project contains a function that returns a couple of random numbers using numpy.random.uniform .

Regardless of other uses of numpy.random in the python process, series of the function calls in both of the runs generate the same sequences, until some point.

And after generating different results for one time at that point, they generate the same sequences again, for some period.

I haven't tried using numpy.random.RandomState yet, but how is this possible?

Is it just a coincidence that somewhere something which uses numpy.random caused the discrepancy and fixed it again?

I'm curious if it is the only possibility or there is another explanation.

Thanks in advance.

ADD: I forgot to mention that there was no seeding at that point.

When you use the random module in numpy, each randomly generated number (regardless of the distribution/function) uses the same "global" instance of RandomState . When you set the seed using numpy.random.seed() , you set the seed of the 'global' instance of RandomState . This is the same principle as the random library in Python.

I'm not sure of the specific implementation of the numpy random functions, but I suspect that each random function will make the underlying Mersenne Twister advance a number of 'steps', with the number of steps not necessarily being the same between different random functions.

So, if the order of every call to a random function is not the same between separate runs, then you may see divergence in the generated sequence of random numbers, with convergence again if the Mersenne Twister 'steps' line up again.

You could get around this by initialising a separate RandomState instance for each function you are using. For example:

import numpy as np

seed = 12345
r_uniform = np.random.RandomState(seed)
r_randint = np.random.RandomState(seed)

a_random_uniform_number = r_uniform.uniform()
a_random_int = r_randint.randint(10)

You might want to set different seeds for each instance - this will depend on what you are using these pseudo-random numbers for.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM