Python random.setstate() Examples
The following are 30
code examples of random.setstate().
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example.
You may also want to check out all available functions/classes of the module
random
, or try the search function
.

Example #1
Source File: __init__.py From picklable-itertools with MIT License | 6 votes |
def verify_tee(n, original, seed): try: state = random.getstate() iterators = list(tee(original, n=n)) results = [[] for _ in range(n)] exhausted = [False] * n while not all(exhausted): # Upper argument of random.randint is inclusive. Argh. i = random.randint(0, n - 1) if not exhausted[i]: if len(results[i]) == len(original): assert_raises(StopIteration, next, iterators[i]) assert results[i] == original exhausted[i] = True else: if random.randint(0, 1): iterators[i] = cPickle.loads( cPickle.dumps(iterators[i])) elem = next(iterators[i]) results[i].append(elem) finally: random.setstate(state)
Example #2
Source File: penis.py From 26-Cogs with GNU General Public License v3.0 | 6 votes |
def penis(self, ctx, *users: discord.Member): """Detects user's penis length This is 100% accurate. Enter multiple users for an accurate comparison!""" if not users: await self.bot.send_cmd_help(ctx) return dongs = {} msg = "" state = random.getstate() for user in users: random.seed(user.id) dongs[user] = "8{}D".format("=" * random.randint(0, 30)) random.setstate(state) dongs = sorted(dongs.items(), key=lambda x: x[1]) for user, dong in dongs: msg += "**{}'s size:**\n{}\n".format(user.display_name, dong) for page in pagify(msg): await self.bot.say(page)
Example #3
Source File: runner.py From skeltorch with MIT License | 6 votes |
def load_states(self, epoch, device): """Loads the states from the checkpoint associated with ``epoch``. Args: epoch (int): ``--epoch`` command argument. device (str): ``--device`` command argument. """ checkpoint_data = self.experiment.checkpoint_load(epoch, device) if isinstance(self.model, torch.nn.DataParallel): self.model.module.load_state_dict(checkpoint_data['model']) else: self.model.load_state_dict(checkpoint_data['model']) self.optimizer.load_state_dict(checkpoint_data['optimizer']) random.setstate(checkpoint_data['random_states'][0]) np.random.set_state(checkpoint_data['random_states'][1]) torch.set_rng_state(checkpoint_data['random_states'][2].cpu()) if torch.cuda.is_available() and checkpoint_data['random_states'][3] is not None: torch.cuda.set_rng_state(checkpoint_data['random_states'][3].cpu()) self.counters = checkpoint_data['counters'] if 'losses' in checkpoint_data: # Compatibility purposes until next release self.losses_epoch = checkpoint_data['losses'] else: self.losses_epoch = checkpoint_data['losses_epoch'] self.losses_it = checkpoint_data['losses_it'] self.load_states_others(checkpoint_data)
Example #4
Source File: solution.py From tiny_python_projects with MIT License | 6 votes |
def test_scramble(): """Test scramble""" state = random.getstate() random.seed(1) assert scramble("a") == "a" assert scramble("ab") == "ab" assert scramble("abc") == "abc" assert scramble("abcd") == "acbd" assert scramble("abcde") == "acbde" assert scramble("abcdef") == "aecbdf" assert scramble("abcde'f") == "abcd'ef" random.setstate(state) # --------------------------------------------------
Example #5
Source File: utils.py From lang2program with Apache License 2.0 | 6 votes |
def random_seed(seed=None): """Execute code inside this with-block using the specified seed. If no seed is specified, nothing happens. Does not affect the state of the random number generator outside this block. Not thread-safe. Args: seed (int): random seed """ if seed is None: yield else: py_state = random.getstate() # save state np_state = np.random.get_state() random.seed(seed) # alter state np.random.seed(seed) yield random.setstate(py_state) # restore state np.random.set_state(np_state)
Example #6
Source File: utils.py From lang2program with Apache License 2.0 | 6 votes |
def random_seed(seed=None): """Execute code inside this with-block using the specified seed. If no seed is specified, nothing happens. Does not affect the state of the random number generator outside this block. Not thread-safe. Args: seed (int): random seed """ if seed is None: yield else: py_state = random.getstate() # save state np_state = np.random.get_state() random.seed(seed) # alter state np.random.seed(seed) yield random.setstate(py_state) # restore state np.random.set_state(np_state)
Example #7
Source File: utils.py From CSBDeep with BSD 3-Clause "New" or "Revised" License | 6 votes |
def choice(population, k=1, replace=True): ver = platform.sys.version_info if replace and (ver.major,ver.minor) in [(2,7),(3,5)]: # python 2.7 or 3.5 # slow if population is large and not a np.ndarray return list(np.random.choice(population, k, replace=replace)) else: try: # save state of 'random' and set seed using 'np.random' state = random.getstate() random.seed(np.random.randint(np.iinfo(int).min, np.iinfo(int).max)) if replace: # sample with replacement return random.choices(population, k=k) else: # sample without replacement return random.sample(population, k=k) finally: # restore state of 'random' random.setstate(state)
Example #8
Source File: _simple_replay_buffer_with_rand_state.py From rl_swiss with MIT License | 6 votes |
def __init__( self, max_rb_size_per_task, observation_dim, action_dim, discrete_action_dim=False, policy_uses_pixels=False, policy_uses_task_params=False, concat_task_params_to_policy_obs=False, random_seed=2001 ): prev_py_rand_state = python_random.getstate() python_random.seed(random_seed) self._py_rand_state = python_random.getstate() python_random.setstate(prev_py_rand_state) self._obs_dim = observation_dim self._act_dim = action_dim self._max_rb_size_per_task = max_rb_size_per_task self._disc_act_dim = discrete_action_dim self._policy_uses_pixels = policy_uses_pixels self._policy_uses_task_params = policy_uses_task_params self._concat_task_params_to_policy_obs = concat_task_params_to_policy_obs p = self._get_partial() self.task_replay_buffers = defaultdict(p)
Example #9
Source File: test_dataloader.py From cotk with Apache License 2.0 | 6 votes |
def base_test_get_batches(self, lp: LanguageProcessing): lp_cp = copy.deepcopy(lp) for set_name in lp.data.keys(): #rng_state = random.getstate() lp_batches = iter(lp.get_batches(set_name, 3, False)) #random.setstate(rng_state) lp_cp.restart(set_name, 3, False) while True: res_cp = lp_cp.get_next_batch(set_name) if res_cp is None: break res = next(lp_batches) assert sorted(res_cp.keys()) == sorted(res.keys()) for key in res_cp.keys(): if isinstance(res_cp[key], np.ndarray): assert (res_cp[key] == res[key]).all() else: assert res_cp[key] == res[key]
Example #10
Source File: test_data.py From Carnets with BSD 3-Clause "New" or "Revised" License | 6 votes |
def test_download_parallel_partial_success_lock_safe(temp_cache, valid_urls, invalid_urls): """Check that a partially successful parallel download leaves the cache unlocked. This needs to be repeated many times because race conditions are what cause this sort of thing, especially situations where a process might be forcibly shut down while it holds the lock. """ s = random.getstate() try: random.seed(0) for _ in range(N_PARALLEL_HAMMER): td = list(islice(valid_urls, FEW)) u_bad = next(invalid_urls) urls = [u_bad] + [u for (u, c) in td] random.shuffle(urls) with pytest.raises(urllib.request.URLError): download_files_in_parallel(urls) finally: random.setstate(s)
Example #11
Source File: dataset.py From intent_classifier with Apache License 2.0 | 6 votes |
def batch_generator(self, batch_size: int, data_type: str = 'train') -> Generator: r"""This function returns a generator, which serves for generation of raw (no preprocessing such as tokenization) batches Args: batch_size (int): number of samples in batch data_type (str): can be either 'train', 'test', or 'valid' Returns: batch_gen (Generator): a generator, that iterates through the part (defined by data_type) of the dataset """ data = self.data[data_type] data_len = len(data) order = list(range(data_len)) rs = random.getstate() random.setstate(self.random_state) random.shuffle(order) self.random_state = random.getstate() random.setstate(rs) for i in range((data_len - 1) // batch_size + 1): yield list(zip(*[data[o] for o in order[i * batch_size:(i + 1) * batch_size]]))
Example #12
Source File: worker_no_timelimit.py From Auto-PyTorch with Apache License 2.0 | 6 votes |
def optimize_pipeline(self, config, budget, config_id, random_state): random.setstate(random_state) if self.permutations is not None: current_sh_run = config_id[0] self.pipeline_config["dataset_order"] = self.permutations[current_sh_run%len(self.permutations)].tolist() try: self.autonet_logger.info("Fit optimization pipeline") return self.pipeline.fit_pipeline(hyperparameter_config=config, pipeline_config=self.pipeline_config, X_train=self.X_train, Y_train=self.Y_train, X_valid=self.X_valid, Y_valid=self.Y_valid, budget=budget, budget_type=self.budget_type, max_budget=self.max_budget, config_id=config_id, working_directory=self.working_directory), random.getstate() except Exception as e: if 'use_tensorboard_logger' in self.pipeline_config and self.pipeline_config['use_tensorboard_logger']: import tensorboard_logger as tl tl.log_value('Exceptions/' + str(e), budget, int(time.time())) #self.autonet_logger.exception('Exception occurred') raise e
Example #13
Source File: checkpoint.py From neat-python with BSD 3-Clause "New" or "Revised" License | 5 votes |
def restore_checkpoint(filename): """Resumes the simulation from a previous saved point.""" with gzip.open(filename) as f: generation, config, population, species_set, rndstate = pickle.load(f) random.setstate(rndstate) return Population(config, (population, species_set, generation))
Example #14
Source File: iterator.py From decaNLP with BSD 3-Clause "New" or "Revised" License | 5 votes |
def use_internal_state(self): """Use a specific RNG state.""" old_state = random.getstate() random.setstate(self._random_state) yield self._random_state = random.getstate() random.setstate(old_state)
Example #15
Source File: test_histogram.py From appmetrics with Apache License 2.0 | 5 votes |
def tearDown(self): random.setstate(self.state)
Example #16
Source File: test_histogram.py From appmetrics with Apache License 2.0 | 5 votes |
def tearDown(self): random.setstate(self.state)
Example #17
Source File: test_histogram.py From appmetrics with Apache License 2.0 | 5 votes |
def tearDown(self): self.patch.stop() random.setstate(self.state)
Example #18
Source File: tune.py From glas with Apache License 2.0 | 5 votes |
def load_tuning(): """ Load any previous tuning information """ filename = tuning_filename() try: stream = open(filename, 'r') except IOError: logging.info('No previous tuning file found.') return {'trials': []} tuning = json.load(stream) random.setstate(pickle.loads(tuning['rng_state'])) cleanup_trials(tuning['trials']) return tuning
Example #19
Source File: fixtures.py From designate with Apache License 2.0 | 5 votes |
def random_seed(seed): """Context manager to set random.seed() temporarily """ state = random.getstate() random.seed(seed) yield random.setstate(state)
Example #20
Source File: training.py From torchsupport with MIT License | 5 votes |
def read(self, path): data = torch.load(path) torch.random.set_rng_state(data["_torch_rng_state"]) np.random.set_state(data["_np_rng_state"]) random.setstate(data["_random_rng_state"]) for param in self.checkpoint_parameters: param.read_action(self, data)
Example #21
Source File: training.py From torchsupport with MIT License | 5 votes |
def save_tick(self, step=None): step = step or self.save_interval this_tick = time.monotonic() if this_tick - self.last_tick > step: try: self.save() self.last_tick = this_tick except SaveStateError: torch_rng_state = torch.random.get_rng_state() np_rng_state = np.random.get_state() random_rng_state = random.getstate() self.load() torch.random.set_rng_state(torch_rng_state) np.random.set_state(np_rng_state) random.setstate(random_rng_state)
Example #22
Source File: cftp.py From pywonderland with MIT License | 5 votes |
def run_cftp(mc): """ Sample a random state in a finite, irreducible Markov chain from its stationary distribution using monotone CFTP. `mc` is a Markov chain object that implements the following methods: 1. `new_random_update`: return a new random updating operation. 2. `update`: update a state by an updating operation. 3. `min_max_state`: return the minimum and maximum states. """ bar = tqdm(desc="Running cftp", unit=" steps") updates = [(random.getstate(), 1)] while True: # run two versions of the chain from the two min, max states # in each round. s0, s1 = mc.min_max_states rng_next = None for rng, steps in updates: random.setstate(rng) for _ in range(steps): u = mc.new_random_update() mc.update(s0, u) mc.update(s1, u) bar.update(1) # save the latest random seed for future use. if rng_next is None: rng_next = random.getstate() # check if these two chains are coupled at time 0. if s0 == s1: break # if not coupled the look further back into the past. else: updates.insert(0, (rng_next, 2**len(updates))) random.setstate(rng_next) bar.close() return s0
Example #23
Source File: tree.py From Recursive-neural-networks-TensorFlow with MIT License | 5 votes |
def simplified_data(num_train, num_dev, num_test): rndstate = random.getstate() random.seed(0) trees = loadTrees('train') + loadTrees('dev') + loadTrees('test') #filter extreme trees pos_trees = [t for t in trees if t.root.label==4] neg_trees = [t for t in trees if t.root.label==0] #binarize labels binarize_labels(pos_trees) binarize_labels(neg_trees) #split into train, dev, test print len(pos_trees), len(neg_trees) pos_trees = sorted(pos_trees, key=lambda t: len(t.get_words())) neg_trees = sorted(neg_trees, key=lambda t: len(t.get_words())) num_train/=2 num_dev/=2 num_test/=2 train = pos_trees[:num_train] + neg_trees[:num_train] dev = pos_trees[num_train : num_train+num_dev] + neg_trees[num_train : num_train+num_dev] test = pos_trees[num_train+num_dev : num_train+num_dev+num_test] + neg_trees[num_train+num_dev : num_train+num_dev+num_test] random.shuffle(train) random.shuffle(dev) random.shuffle(test) random.setstate(rndstate) return train, dev, test
Example #24
Source File: random.py From PyTorch-NLP with BSD 3-Clause "New" or "Revised" License | 5 votes |
def set_random_generator_state(state): """ Set the `torch`, `numpy` and `random` random generator state. Args: state (RandomGeneratorState) """ random.setstate(state.random) torch.random.set_rng_state(state.torch) np.random.set_state(state.numpy) if state.torch_cuda is not None and torch.cuda.is_available() and len( state.torch_cuda) == torch.cuda.device_count(): # pragma: no cover torch.cuda.set_rng_state_all(state.torch_cuda)
Example #25
Source File: util.py From macarico with MIT License | 5 votes |
def restore_checkpoint(self, filename): self.print_it('restoring model from %s...' % filename) checkpoint = torch.load(filename) 'TrainLoopStatus' (self.tr_loss_matrix, self.de_loss_matrix, self.epoch, self.n_training_ex, self.example_order, self.M, self.N, self.N_print, self.N_last, self.objective_average, self.final_parameters, self.best_de_err, self.print_history, ) = checkpoint['TrainLoopStatus'] self.policy.load_state_dict(checkpoint['policy']) self.optimizer.load_state_dict(checkpoint['optimizer']) torch.random.set_rng_state(checkpoint['torch_random_state']) np.random.set_state(checkpoint['np_random_state']) py_random.setstate(checkpoint['py_random_state']) max_len = max((len(s) for s,_,_ in self.print_history)) for string, args, kwargs in self.print_history: print(string + (' ' * (max_len - len(string))) + ' o_o', *args, **kwargs)
Example #26
Source File: random.py From chainer with MIT License | 5 votes |
def _numpy_do_teardown(): global _old_python_random_state global _old_numpy_random_state random.setstate(_old_python_random_state) numpy.random.set_state(_old_numpy_random_state) _old_python_random_state = None _old_numpy_random_state = None
Example #27
Source File: generator.py From blocksmith with Apache License 2.0 | 5 votes |
def __generate_big_int(self): if self.prng_state is None: seed = int.from_bytes(self.pool, byteorder='big', signed=False) random.seed(seed) self.prng_state = random.getstate() random.setstate(self.prng_state) big_int = random.getrandbits(self.KEY_BYTES * 8) self.prng_state = random.getstate() return big_int
Example #28
Source File: seed.py From evmlab with GNU General Public License v3.0 | 5 votes |
def set_compressed_random_state(state): random.setstate(pickle.loads(zlib.decompress(base64.b64decode(state))))
Example #29
Source File: random.py From cupy with MIT License | 5 votes |
def do_teardown(): global _old_python_random_state global _old_numpy_random_state global _old_cupy_random_states random.setstate(_old_python_random_state) numpy.random.set_state(_old_numpy_random_state) cupy.random.generator._random_states = _old_cupy_random_states _old_python_random_state = None _old_numpy_random_state = None _old_cupy_random_states = None # In some tests (which utilize condition.repeat or condition.retry), # setUp/tearDown is nested. _setup_random() and _teardown_random() do their # work only in the outermost setUp/tearDown pair.
Example #30
Source File: DistributedGameTable.py From Pirates-Online-Rewritten with BSD 3-Clause "New" or "Revised" License | 5 votes |
def createAiPlayerName(self, female, seed): state = random.getstate() random.seed(seed) if female: first_name_array = PLocalizer.PirateNames_FirstNamesFemale else: first_name_array = PLocalizer.PirateNames_FirstNamesMale last_name_prefix_array = PLocalizer.PirateNames_LastNamePrefixesGeneric last_name_suffix_array = PLocalizer.PirateNames_LastNameSuffixesGeneric string = '' string = string + self.randomArraySelection(first_name_array) + ' ' string = string + self.randomArraySelection(last_name_prefix_array) string = string + self.randomArraySelection(last_name_suffix_array) random.setstate(state) return string