简体   繁体   中英

Why does numpy RandomState gives different results?

I don't understand why

import numpy as np
rng = np.random.RandomState(42)
rng.randint(10, size=1)
np.random.RandomState(rng.randint(10, size=1)).randint(10, size=3)

>>> OUTPUT: array([8, 9, 3])

and

import numpy as np
np.random.RandomState(42).randint(10, size=1)
np.random.RandomState(np.random.RandomState(42).randint(10, size=1)).randint(10,size=3)
>>> OUTPUT: array([9, 3, 4])

Can someone please explain the difference?

Please Note Before Reading: It should be minded that all the below instances of the word "random" mean "pseudo-random".

The simple answer is because in you are using a different seeds for different calls and subsequent operations.

In the example you have provided

import numpy as np
rng = np.random.RandomState(42)
rng.randint(10, size=1) # This is the first time you are asking 'rng' for 'randint()' with seed 42 which will return 6. First Random Number.
np.random.RandomState(rng.randint(10, size=1)).randint(10, size=3) # This is the second time you are asking the same 'rng' for 'randint()' with seed 42 which will return 3. Second Random Number.

In the second example you have provided,

import numpy as np
np.random.RandomState(42).randint(10, size=1) # this is the first np.random.RandomState(42).randint(), which is 6, same as above
np.random.RandomState(np.random.RandomState(42).randint(10, size=1)).randint(10,size=3) # this is still the first np.random.RandomState(42).randint() , which is 6 again, but it was 3 above

The compatibility guarantee stated in the documentation is this:

Compatibility Guarantee

A fixed bit generator using a fixed seed and a fixed series of calls to 'RandomState' methods using the same parameters will always produce the same results up to roundoff error except when the values were incorrect. RandomState is effectively frozen and will only receive updates that are required by changes in the the internals of Numpy. More substantial changes, including algorithmic improvements, are reserved for Generator.

It means when you use the same seed, same series of numbers will be provided, ie the first random number after the initialization of the constructor of np.random.RandomState(42) will always be 6, the second random number will always be 3 as long as you are requesting for one random value.

Verify by running this.

import numpy as np
rng = np.random.RandomState(42)
print(rng.randint(10, size=1)) # output is always [6]
print(rng.randint(10, size=1)) # output is always [3]

I hope this answers your question.

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