2

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?

3
  • 1
    random is supposed to generate random numbers. What exactly is your question here? If you wish to generate the same set of random numbers each time you have to set seed. more information Commented Jun 9, 2021 at 13:37
  • 1
    In your first piece of code, you're generating a random integer at line 3 and then again at line 4. Comment line 3 and you'll have the same output for both. Commented Jun 9, 2021 at 13:38
  • @not_speshal Thanks! I think I understand now I thout that running rng.randint(10, size=1 should always return the exact same number. Commented Jun 9, 2021 at 13:51

1 Answer 1

2

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, i.e. 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.

Sign up to request clarification or add additional context in comments.

1 Comment

Thank you for your reply! Now I think I understand how RandomState actually works.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.