Direct way to access Numpy RandomState object
Is there a more direct way to access the object RandomState
created on import other than np.random.<some function>.__self__
? Both np.random._rand
and getattr(np.random, "_rand")
improve AttributeError
. The first works fine, but doesn't seem very transparent / Pythonic, although the most transparent might just be creating a separate object RandomState
. The goal is to pass a variable interal_state
to the cython function that calls the randomkit functions directly.
source to share
You can use np.random.get_state()
to access the random state and np.random.set_state()
to set it.
Usage example:
>>> import numpy as np
>>> state = np.random.get_state()
>>> np.random.rand()
0.5951085367670415
>>> np.random.set_state(state)
>>> np.random.rand()
0.5951085367670415
Note that state
- this is just a tuple
>>> state
('MT19937', array([3133054952, 999208925, 1226199620, ..., 3991712371, 943990344,
955790602], dtype=uint32), 624, 0, 0.0)
source to share