def random_init(): """ Initialize the random states with system entropy. """ b = os.urandom(N * 4) for n in ('py_random_state', 'np_random_state'): _helperlib.rnd_seed(_helperlib.c_helpers[n], b)
def _check_perturb(self, ptr): states = [] for i in range(10): # Initialize with known state _helperlib.rnd_seed(ptr, 0) # Perturb with entropy _helperlib.rnd_seed(ptr, os.urandom(512)) states.append(tuple(_helperlib.rnd_get_state(ptr)[1])) # No two identical states self.assertEqual(len(set(states)), len(states))
def random_init(): """ Initialize the random states with system entropy. """ global _pid if _pid != os.getpid(): b = os.urandom(N * 4) for n in ('py_random_state', 'np_random_state'): _helperlib.rnd_seed(_helperlib.c_helpers[n], b) _pid = os.getpid()
def _check_init(self, ptr): # We use the same integer seeding as Numpy # (CPython is different: it treats the integer as a byte array) r = np.random.RandomState() for i in [0, 1, 125, 2**32 - 5]: # Need to cast to a C-sized int (for Numpy <= 1.7) r.seed(np.uint32(i)) st = r.get_state() ints = list(st[1]) index = st[2] assert index == N # sanity check _helperlib.rnd_seed(ptr, i) self.assertEqual(_helperlib.rnd_get_state(ptr), (index, ints))