Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to access numpy default global random number generator

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

like image 792
Atimaharjun Avatar asked Feb 01 '17 17:02

Atimaharjun


People also ask

How do I get a Numpy random array?

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.

Is Numpy random seed Global?

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.

Which random number generator does Numpy use?

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.


1 Answers

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__
like image 130
user2357112 supports Monica Avatar answered Oct 04 '22 11:10

user2357112 supports Monica