Ejemplo n.º 1
0
def _copy_np_state(r, ptr):
    """
    Copy state of Numpy random *r* to Numba state *ptr*.
    """
    ints, index = r.get_state()[1:3]
    _helperlib.rnd_set_state(ptr, (index, [int(x) for x in ints]))
    return ints, index
Ejemplo n.º 2
0
def _copy_np_state(r, ptr):
    """
    Copy state of Numpy random *r* to Numba state *ptr*.
    """
    ints, index = r.get_state()[1:3]
    _helperlib.rnd_set_state(ptr, (index, [int(x) for x in ints]))
    return ints, index
Ejemplo n.º 3
0
def _copy_py_state(r, ptr):
    """
    Copy state of Python random *r* to Numba state *ptr*.
    """
    mt = r.getstate()[1]
    ints, index = mt[:-1], mt[-1]
    _helperlib.rnd_set_state(ptr, (index, list(ints)))
    return ints, index
Ejemplo n.º 4
0
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
Ejemplo n.º 5
0
def _copy_py_state(r, ptr):
    """
    Copy state of Python random *r* to Numba state *ptr*.
    """
    mt = r.getstate()[1]
    ints, index = mt[:-1], mt[-1]
    _helperlib.rnd_set_state(ptr, (index, list(ints)))
    return ints, index
Ejemplo n.º 6
0
 def _check_get_set_state(self, ptr):
     state = _helperlib.rnd_get_state(ptr)
     i, ints = state
     self.assertIsInstance(i, int)
     self.assertIsInstance(ints, list)
     self.assertEqual(len(ints), N)
     j = (i * 100007) % N
     ints = [i * 3 for i in range(N)]
     # Roundtrip
     _helperlib.rnd_set_state(ptr, (j, ints))
     self.assertEqual(_helperlib.rnd_get_state(ptr), (j, ints))
Ejemplo n.º 7
0
 def _check_get_set_state(self, ptr):
     state = _helperlib.rnd_get_state(ptr)
     i, ints = state
     self.assertIsInstance(i, int)
     self.assertIsInstance(ints, list)
     self.assertEqual(len(ints), N)
     j = (i * 100007) % N
     ints = [i * 3 for i in range(N)]
     # Roundtrip
     _helperlib.rnd_set_state(ptr, (j, ints))
     self.assertEqual(_helperlib.rnd_get_state(ptr), (j, ints))
Ejemplo n.º 8
0
 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)
Ejemplo n.º 9
0
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)