import numpy as np
rng=np.random.RandomState(123)
for i in np.arange(1,21):
number=2**i
heads_tails=rng.randint(0,2,size=number)
ptails=np.mean(heads_tails)
print("Number of tosses:",number,"---","Probability of Tails:%.2f"%(ptails*100),"%","---",
"Probabilty of Heads:%.2f"%(100-ptails*100),"%")
Hi,I wrote this code and I didn't understand the random.RandomState's function in here,What is it using for or What is the function of that? If you can explain,I would be appreciated.
np.random.RandomState()
constructs a random number generator. np.random.RandomState()
returns a new seeded RandomState
instance but otherwise does not change anything. You have to use the returned RandomState
instance to get consistent pseudorandom numbers.
For instance, if you use the functions in the numpy.random
you will not get consistent pseudorandom numbers because they are pulling from a different RandomState
instance than the one you just created. So to be consistent, you can create a specific RandomState
and draw to pseudorandom numbers in a reproducible way.