I am aware of suggested best practices for random number generation in NumPy, which basically boil down to:
- Use the new API, via
np.random.default_rng
and related functions; and - Set a seed at the beginning of your program to ensure reproducibility throughout.
My question is, what if we want to ensure equal results from random number generation within individual functions? With the goal being that you give a generator/seed argument at every function call and every time you pass it the same generator/seed, you get equivalent results.
My initial thought was to use an rng
variable in the function body as you would normally (if you were setting and fetting at the start of your script) and then either set a function parameter like rng=np.random.default_rng()
expecting a Generator object or seed
(to be passed to an np.random.default_rng(seed)
call at the start of the function body).
However, this seems like it goes against best practices as you would be creating multiple, local, temporary Generator objects. What is the "proper" solution to this problem?