I have a simulation that requires the generation of N
random numbers each iteration. However, N
varies depending on the state of the simulation. Because N
varies, the following calls to numpy.random()
yield different results. However, I would like the following calls to numpy.random()
to be uninfluenced by N
.
I have been solving this by (1) generating a large random integer before the numpy.random()
call and then (2) setting the random seed as this integer following the numpy.random()
call (see below). However this seems clumsy.
Is there a better way to control numpy.random's state that is uninfluenced by the size
argument?
import numpy as np
N=10
# set seed
np.random.seed(0)
# generate N numbers
a = np.random.normal(size=N)
# check its state
print(np.random.randint(1e4))
This call to randint()
returns 2599
# set seed
N = 1
np.random.seed(0)
# generate N numbers
b = np.random.normal(size=N)
# check its state
print(np.random.randint(1e4))
This call to randint()
returns 4859
Potential solution:
N = 10
# set seed
np.random.seed(0)
# generate a large random integer
rand_int = np.random.randint(1e6)
# generate N numbers
b = np.random.normal(size=N)
# reset the seed
np.random.seed(rand_int)
# check its state
print(np.random.randint(1e4))
This call to randint()
returns 5143
, independent of N
Aucun commentaire:
Enregistrer un commentaire