The fact that NumPy now recommends that new code uses the defacult_rng()
instance instead of numpy.random
for new code has got me thinking about how it should be used to yield good results, both performance vice and statistically.
This first example is how I first wanted to write:
import numpy as np
class fancy_name():
def __init__(self):
self.rg = np.random.default_rng()
self.gamma_shape = 1.0
self.gamma_scale = 1.0
def public_method(self, input):
# Do intelligent stuff with input
return self.rg.gamma(self.gamma_shape, slef.gamma_scale)
But I have also considered creating a new instance in every function call:
import numpy as np
class fancy_name():
def __init__(self):
self.gamma_shape = 1.0
self.gamma_scale = 1.0
def public_method(self, input):
# Do intelligent stuff with input
rg = np.random.default_rng()
return rg.gamma(self.gamma_shape, slef.gamma_scale)
A third alternative would be to pass the rng as an argument in the function call. This way the same rng can be used in other parts of the code as well.
This is used in a simulation environment that is going to be called often to sample, for example, transition times.
I guess the question is if there are arguments for any of these three methods and if there exists some kind of praxis?
Also, any reference to more in-depth explanations of using these random number generators (except for the NumPy doc and Random Sampling article) is of great interest!