def test_reset_state_uint32(self): rg = RandomGenerator(self.brng(*self.seed)) rg.randint(0, 2**24, 120, dtype=np.uint32) state = rg.state n1 = rg.randint(0, 2**24, 10, dtype=np.uint32) rg2 = RandomGenerator(self.brng()) rg2.state = state n2 = rg2.randint(0, 2**24, 10, dtype=np.uint32) assert_array_equal(n1, n2)
def test_array(self): s = RandomGenerator(MT19937(range(10))) assert_equal(s.randint(1000), 468) s = np.random.RandomState(range(10)) assert_equal(s.randint(1000), 468) s = RandomGenerator(MT19937(np.arange(10))) assert_equal(s.randint(1000), 468) s = RandomGenerator(MT19937([0])) assert_equal(s.randint(1000), 973) s = RandomGenerator(MT19937([4294967295])) assert_equal(s.randint(1000), 265)
def test_state_tuple(self): rs = RandomGenerator(self.brng(*self.data1['seed'])) brng = rs.brng state = brng.state desired = rs.randint(2 ** 16) tup = (state['brng'], state['state']['key'], state['state']['pos']) brng.state = tup actual = rs.randint(2 ** 16) assert_equal(actual, desired) tup = tup + (0, 0.0) brng.state = tup actual = rs.randint(2 ** 16) assert_equal(actual, desired)
def test_scalar(self): s = RandomGenerator(MT19937(0)) assert_equal(s.randint(1000), 684) s1 = np.random.RandomState(0) assert_equal(s1.randint(1000), 684) assert_equal(s1.randint(1000), s.randint(1000)) s = RandomGenerator(MT19937(4294967295)) assert_equal(s.randint(1000), 419) s1 = np.random.RandomState(4294967295) assert_equal(s1.randint(1000), 419) assert_equal(s1.randint(1000), s.randint(1000)) self.rg.seed(4294967295) self.nprs.seed(4294967295) self._is_state_common()
instance_amnt = int(input("# of instances for each: ")) bits_per_instance = int(input("# of bits per instance: ")) sp800_22_tests.append_header("cluster_data_combined_2.csv") rndP = RandomGenerator(Philox()) rndX = RandomGenerator() rndT = RandomGenerator(ThreeFry()) for s in range(instance_amnt): print("---------------------- Iteration " + str(s) + " ----------------------") #Generating Random Numbers with each of the different PRNGs bitsP = list(rndP.randint(low=0, high=2, size=bits_per_instance)) bitsX = list(rndX.randint(low=0, high=2, size=bits_per_instance)) bitsT = list(rndT.randint(low=0, high=2, size=bits_per_instance)) bitsM = [] for r in range(bits_per_instance): bitsM.append(random.randrange(0,2)) #Running NIST suite tests on the generated data #IMPORTANT: the data in the csv is not organized in blocks: Instead, #if x=entryNum%4, then x = 1 is Philox, x = 2 is Xorishiro, #x = 3 is ThreeFry, and x=4 is Mersenne Twister print("-------- Philox " + str(s) +" --------") sp800_22_tests.test_func(bitsP, "cluster_data_combined_2.csv") print("-------- Xoroshiro " + str(s) +" --------") sp800_22_tests.test_func(bitsX, "cluster_data_combined_2.csv") print("-------- ThreeFry " + str(s) +" --------")
def run_all(self, processors=1, iseed=1): """Run the model for all trials in the designed experiment and store results. Model constructor is assumed to take args (seed, collect_stepwise_data, trial kwargs). Args: processors (int, default=1): Number of cpu cores to use for batch run. iseed (int, default=1): Initial seed for replication 1 of each trial. Seeds for subsequent replications will be PNRG by this class. """ pool = ProcessPool(nodes=processors) job_queue = [] # Generator for initial model seeds. Models use these seeds to manage # their own RNGs. brng = PCG64(iseed) randomgen = RandomGenerator(brng) param_names = self.design.columns param_names = param_names[(param_names != 'replications') & (param_names != 'Trial')] total_iterations = self.design.replications.sum() self.manifest = [] # Records what seed was used where for row in self.design.itertuples(): kwargs = {key: getattr(row, key) for key in param_names} # Reset model seed generator for next design point brng.seed(iseed) for rep in range(1, row.replications + 1): # Advance model seed for next replication model_seed = randomgen.randint(10000000) model_key = ( row.Trial, rep, ) self.manifest.append((model_key, model_seed)) job_queue.append( pool.uimap(self._run_single_replication, (model_seed, ), (kwargs, ), (model_key, ))) with tqdm(total=total_iterations, desc='Total', unit='dp', disable=not self.display_progress) as pbar_total: # empty the queue results = [] for task in job_queue: for model_vars, agent_vars, stepwise_vars in list(task): results.append((model_vars, agent_vars, stepwise_vars)) pbar_total.update() if self.data_handler: return # Results already stored to database, nothing more to record # store the results in batchrunner # FUTURE: rework this module to only support external data_handler. # Rationale: Best practice to treat each replication atomically. Key # benefit of having all replications in memory is to do experiment-wide # analysis, and a data analysis module can read in all per-replication # files. for model_vars, agent_vars, stepwise_vars in results: if self.model_reporters: for model_key, model_val in model_vars.items(): self.model_vars[model_key] = model_val if self.agent_reporters: for agent_key, reports in agent_vars.items(): self.agent_vars[agent_key] = reports if self.collect_stepwise_data: for stepwise_key, stepwise_val in stepwise_vars.items(): self.stepwise_vars[stepwise_key] = stepwise_val