I need to create a class which takes in a random number generator (i.e. a numpy.random.RandomState
object) as a parameter. In the case this argument is not specified, I would like to assign it to the random generator that numpy uses when we run numpy.random.<random-method>
. How do I access this global generator? Currently I am doing this by just assigning the module object as the random generator (since they share methods / duck typing). However this causes issues when pickling (unable to pickle module object) and deep-copying. I would like to use the RandomState object behind numpy.random
PS: I'm using python-3.4
The choice() method allows you to generate a random value based on an array of values. The choice() method takes an array as a parameter and randomly returns one of the values.
seed(number) sets what NumPy calls the global random seed, which affects all uses to the np. random. * module. Some imported packages or other scripts could reset the global random seed to another random seed with np.
It uses Mersenne Twister, and this bit generator can be accessed using MT19937 . Generator , besides being NumPy-aware, has the advantage that it provides a much larger number of probability distributions to choose from.
As well as what kazemakase suggests, we can take advantage of the fact that module-level functions like numpy.random.random
are really methods of a hidden numpy.random.RandomState
by pulling the __self__
directly from one of those methods:
numpy_default_rng = numpy.random.random.__self__
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With