[Numpy-discussion] python is cool
Neal Becker
ndbecker2 at gmail.com
Tue May 12 14:18:40 EDT 2015
In order to make sure all my random number generators have good
independence, it is a good practice to use a single shared instance (because
it is already known to have good properties). A less-desirable alternative
is to used rng's seeded with different starting states - in this case the
independence properties are not generally known.
So I have some fairly deeply nested data structures (classes) that somewhere
contain a reference to a RandomState object.
I need to be able to clone these data structures, producing new independent
copies, but I want the RandomState part to be the shared, singleton rs
object.
In python, no problem:
---
from numpy.random import RandomState
class shared_random_state (RandomState):
def __init__ (self, rs):
RandomState.__init__(self, rs)
def __deepcopy__ (self, memo):
return self
---
Now I can copy.deepcopy the data structures, but the randomstate part is
shared. I just use
rs = shared_random_state (random.RandomState(0))
and provide this rs to all my other objects. Pretty nice!
--
Those who fail to understand recursion are doomed to repeat it
More information about the NumPy-Discussion
mailing list