def numba_typify_RandomState(state, **kwargs): # The numba_typify in this case is just an passthrough function # that synchronizes Numba's internal random state with the current # RandomState object ints, index = state.get_state()[1:3] ptr = _helperlib.rnd_get_np_state_ptr() _helperlib.rnd_set_state(ptr, (index, [int(x) for x in ints])) return state
def _set_random_state(self): # This uses a trick by Alexandre Gramfort, # see https://github.com/numba/numba/issues/3249 if self._random_state >= 0: if self._using_numba: r = np.random.RandomState(self._random_state) ptr = _helperlib.rnd_get_np_state_ptr() ints, index = r.get_state()[1:3] _helperlib.rnd_set_state(ptr, (index, [int(x) for x in ints])) self._ptr = ptr self._r = r else: np.random.seed(self._random_state)
def box_random_state(typ, val, c): """Convert a native `RandomStateNumbaModel` structure to an `RandomState` object using Numba's internal state array. Note that `RandomStateNumbaModel` is just a placeholder structure with no inherent information about Numba internal random state, all that information is instead retrieved from Numba using ``_helperlib.rnd_get_state()`` and a new `RandomState` is constructed using the Numba's current internal state. """ pos, state_list = _helperlib.rnd_get_state( _helperlib.rnd_get_np_state_ptr()) rng = RandomState() rng.set_state(("MT19937", state_list, pos)) class_obj = c.pyapi.unserialize(c.pyapi.serialize_object(rng)) return class_obj
def set_random_state(_state_tuple): """Set the random state from a tuple. Set the random state from a tuple in the form returned by :func:`numba._helperlib.rnd_get_state`. .. important:: You probably do not need to use this function. Be sure you know *exactly* how you are affecting the random number generator state before using this function, or you are likely to create a model that cannot be peproduced. See also :func:`set_random_seed`. Parameters ---------- state : :obj:`tuple` Random number generator state as a tuple. """ ptr = _helperlib.rnd_get_np_state_ptr() _helperlib.rnd_set_state(ptr, _state_tuple)
def get_random_state(): """Get the random state as a tuple. Get the random state from a tuple in the form returned by :func:`numba._helperlib.rnd_get_state`. This tuple contains the necessary information for resuming a checkpoint from the exact same random number generator state. .. important:: You probably do not need to use this function. Be sure you know *exactly* how you are affecting the random number generator state before using this function, or you are likely to create a model that cannot be peproduced. See also :func:`set_random_seed`. Returns ------- state : :obj:`tuple` Random number generator state as a tuple. """ ptr = _helperlib.rnd_get_np_state_ptr() return _helperlib.rnd_get_state(ptr)
def get_np_state_ptr(): """ Get the Numba state *ptr* """ return _helperlib.rnd_get_np_state_ptr()