Is there any way to use two different seeds for numpy random number generator in a python code, one to be used for part of the code, and the other for the rest of the code?
Asked
Active
Viewed 4,763 times
6
-
a = np.random.mtrand.RandomState(1).randint(0, 10, size=(5,2)) ... b = np.random.mtrand.RandomState(2).randint(0, 10, size=(5,2)) ... try it – NaN May 11 '17 at 23:55
1 Answers
19
You can use several different np.random.RandomState
s and call the methods on those:
import numpy as np
rng1 = np.random.RandomState(100)
rng2 = np.random.RandomState(100)
print(rng1.randint(0, 100, 1)) # [8]
print(rng2.randint(0, 100, 1)) # [8]
I used the same seed (100
) for both, because it shows that both give the same results.

MSeifert
- 145,886
- 38
- 333
- 352
-
it might be interesting to show that my earlier comment produces different but reproducible results if the randomstate is changed. Also the distributions that can be accessed are covered there as well in the help topics in numpy. – NaN May 12 '17 at 00:54
-
@NaN Not sure if I can follow. Do you mean different seeds or the state after the call to `randint` or changing the seed for the state manually? – MSeifert May 12 '17 at 01:11
-
yes, you used the same seed, my example used different ones, both produce reproducible results, in your case 8 for both, in mine, a and b have different values, but their values don't change. Plus randint was only one type of distribution, others are useful (normal, uniform etc) – NaN May 12 '17 at 01:43
-
1I think if someone needs additional information they can follow the link or read the documentation of `RandomState`. I could've included different seeds but the point I was trying to make was that these `RandomState`s have states that are independant of each other. – MSeifert May 12 '17 at 01:49