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: solution.py    From tiny_python_projects with MIT License 6 votes vote down vote up
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 #2
Source File: test_data.py    From Carnets with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
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 #3
Source File: dataset.py    From intent_classifier with Apache License 2.0 6 votes vote down vote up
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 #4
Source File: worker_no_timelimit.py    From Auto-PyTorch with Apache License 2.0 6 votes vote down vote up
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 #5
Source File: __init__.py    From picklable-itertools with MIT License 6 votes vote down vote up
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 #6
Source File: penis.py    From 26-Cogs with GNU General Public License v3.0 6 votes vote down vote up
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 #7
Source File: test_dataloader.py    From cotk with Apache License 2.0 6 votes vote down vote up
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 #8
Source File: _simple_replay_buffer_with_rand_state.py    From rl_swiss with MIT License 6 votes vote down vote up
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: utils.py    From CSBDeep with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
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 #10
Source File: utils.py    From lang2program with Apache License 2.0 6 votes vote down vote up
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 #11
Source File: utils.py    From lang2program with Apache License 2.0 6 votes vote down vote up
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 #12
Source File: runner.py    From skeltorch with MIT License 6 votes vote down vote up
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 #13
Source File: iterator.py    From pytorch-nlp with MIT License 5 votes vote down vote up
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 #14
Source File: ch06_ex2.py    From Mastering-Object-Oriented-Python-Second-Edition with MIT License 5 votes vote down vote up
def __exit__(
            self,
            exc_type: Optional[Type[BaseException]],
            exc_value: Optional[BaseException],
            traceback: Optional[TracebackType]
    ) -> Optional[bool]:
        random.setstate(self.was)
        return False 
Example #15
Source File: ch06_ex2.py    From Mastering-Object-Oriented-Python-Second-Edition with MIT License 5 votes vote down vote up
def __exit__(
            self,
            exc_type: Optional[Type[BaseException]],
            exc_value: Optional[BaseException],
            traceback: Optional[TracebackType]
    ) -> Optional[bool]:
        random.setstate(self.was)
        return False 
Example #16
Source File: base_model.py    From memory-augmented-self-play with MIT License 5 votes vote down vote up
def _load_metadata(self, checkpoint):
        np.random.set_state(checkpoint[NP_RANDOM_STATE])
        random.setstate(checkpoint[PYTHON_RANDOM_STATE])
        torch.set_rng_state(checkpoint[PYTORCH_RANDOM_STATE]) 
Example #17
Source File: base_policy.py    From memory-augmented-self-play with MIT License 5 votes vote down vote up
def _load_metadata(self, checkpoint):
        np.random.set_state(checkpoint[NP_RANDOM_STATE])
        random.setstate(checkpoint[PYTHON_RANDOM_STATE])
        torch.set_rng_state(checkpoint[PYTORCH_RANDOM_STATE]) 
Example #18
Source File: basehandler.py    From streamingbandit with MIT License 5 votes vote down vote up
def temp_seed(self, seed):
        statenp = np.random.get_state()
        state = random.getstate()
        #np.random.seed(seed)
        global_randstate.seed(seed)
        random.seed(seed)
        try:
            yield
        finally:
            np.random.set_state(statenp)
            random.setstate(state) 
Example #19
Source File: distinct.py    From cotk with Apache License 2.0 5 votes vote down vote up
def close(self):
		'''
		Returns:
			(dict): Return a dict which contains

			* **bleu**: bleu value.
			* **bleu hashvalue**: hash value for bleu metric, same hash value stands
			  for same evaluation settings.
		'''
		result = super().close()
		if not self.hyps:
			raise RuntimeError("The metric has not been forwarded data correctly.")

		if self.sample > len(self.hyps):
			sample = len(self.hyps)
		else:
			sample = self.sample
		self._hash_ordered_data(sample)

		rng_state = random.getstate()
		random.seed(self.seed)
		random.shuffle(self.hyps)
		random.setstate(rng_state)
		self.hyps = self.hyps[:sample]

		if self.tokenizer:
			self._do_tokenize()

		if "unk" in self.dataloader.get_special_tokens_mapping():
			self.hyps = replace_unk(self.hyps, unk = self.dataloader.get_special_tokens_mapping().get("unk", None))

		ngram_list = list(chain(*[ngrams(sentence, self.ngram, pad_left=True, pad_right=True) for sentence in self.hyps]))
		ngram_set = set(ngram_list)

		result.update({"distinct": len(ngram_set) / len(ngram_list), \
			"distinct hashvalue": self._hashvalue()})
		return result 
Example #20
Source File: training.py    From sockeye with Apache License 2.0 5 votes vote down vote up
def _load_training_state(self, train_iter: data_io.BaseParallelSampleIter):
        """
        Loads the full training state from disk.
        :param train_iter: training data iterator.
        """
        # (1) Parameters
        params_fname = os.path.join(self.training_state_dirname, C.TRAINING_STATE_PARAMS_NAME)
        self.model.load_parameters(params_fname, ctx=self.context, allow_missing=False, ignore_extra=False)

        # (2) Optimizer states
        opt_state_fname = os.path.join(self.training_state_dirname, C.OPT_STATES_LAST)
        self._load_trainer_states(opt_state_fname)

        # (3) Data Iterator
        train_iter.load_state(os.path.join(self.training_state_dirname, C.BUCKET_ITER_STATE_NAME))

        # (4) Random generators
        # RNG states: python's random and np.random provide functions for
        # storing the state, mxnet does not, but inside our code mxnet's RNG is
        # not used AFAIK
        with open(os.path.join(self.training_state_dirname, C.RNG_STATE_NAME), "rb") as fp:
            random.setstate(pickle.load(fp))
            np.random.set_state(pickle.load(fp))

        # (5) Training state
        self.state = TrainState.load(os.path.join(self.training_state_dirname, C.TRAINING_STATE_NAME))

        # (6) AMP loss scaler state
        if self.using_amp:
            # Load loss scaler state
            with open(os.path.join(self.training_state_dirname, C.AMP_LOSS_SCALER_STATE_NAME), "rb") as fp:
                (self.trainer._amp_loss_scaler._loss_scale,
                 self.trainer._amp_loss_scaler._next_loss_scale,
                 self.trainer._amp_loss_scaler._unskipped) = pickle.load(fp) 
Example #21
Source File: cflw数学_随机.py    From cflw_py with MIT License 5 votes vote down vote up
def fs状态(self, a状态):
		random.setstate(a状态) 
Example #22
Source File: dataset.py    From intent_classifier with Apache License 2.0 5 votes vote down vote up
def __init__(self, data, seed=None, classes=None,
                 fields_to_merge=None, merged_field=None,
                 field_to_split=None, splitted_fields=None, splitting_proportions=None,
                 *args, **kwargs):

        rs = random.getstate()
        random.seed(seed)
        self.random_state = random.getstate()
        random.setstate(rs)

        self.train = data.get('train', [])
        self.test = data.get('test', [])
        self.data = {
            'train': self.train,
            'test': self.test,
            'all': self.train + self.test
        }

        self.classes = classes
        if fields_to_merge is not None:
            if merged_field is not None:
                # print("Merging fields <<{}>> to new field <<{}>>".format(fields_to_merge, merged_field))
                self._merge_data(fields_to_merge=fields_to_merge.split(' '), merged_field=merged_field)
            else:
                raise IOError("Given fields to merge BUT not given name of merged field")

        if field_to_split is not None:
            if splitted_fields is not None:
                # print("Splitting field <<{}>> to new fields <<{}>>".format(field_to_split, splitted_fields))
                self._split_data(field_to_split=field_to_split,
                                 splitted_fields=splitted_fields.split(" "),
                                 splitting_proportions=[float(s) for s in splitting_proportions.split(" ")])
            else:
                raise IOError("Given field to split BUT not given names of splitted fields") 
Example #23
Source File: cm_noiseChannels.py    From CrowdMaster with GNU General Public License v3.0 5 votes vote down vote up
def agentRandom(self, offset=0):
        """Return a random number that is consistent between frames but can
        be offset by an integer"""
        state = random.getstate()
        random.seed(hash(self.userid) - 1 + offset)
        # -1 so that this number is different to the first random number
        # generated on frame 0 (if used) of the simulation
        result = random.random()
        random.setstate(state)
        return result 
Example #24
Source File: fixtures.py    From apm-agent-python with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
def not_so_random():
    old_state = random.getstate()
    random.seed(42)
    yield
    random.setstate(old_state) 
Example #25
Source File: rng.py    From oac-explore with MIT License 5 votes vote down vote up
def set_global_pkg_rng_state(rng_states):

    random.setstate(rng_states['py_rng_state'])

    np.random.set_state(rng_states['np_rng_state'])

    torch.set_rng_state(rng_states['t_cpu_rng_state'])

    if torch.cuda.is_available():
        torch.cuda.set_rng_state_all(rng_states['t_gpu_rng_state']) 
Example #26
Source File: solution2_for_append_list.py    From tiny_python_projects with MIT License 5 votes vote down vote up
def test_choose():
    """Test choose"""

    state = random.getstate()
    random.seed(1)
    assert choose('a') == 'a'
    assert choose('b') == 'b'
    assert choose('c') == 'C'
    assert choose('d') == 'd'
    random.setstate(state)


# -------------------------------------------------- 
Example #27
Source File: solution1_for_loop.py    From tiny_python_projects with MIT License 5 votes vote down vote up
def test_choose():
    """Test choose"""

    state = random.getstate()
    random.seed(1)
    assert choose('a') == 'a'
    assert choose('b') == 'b'
    assert choose('c') == 'C'
    assert choose('d') == 'd'
    random.setstate(state)


# -------------------------------------------------- 
Example #28
Source File: solution5_shorter_list_comp.py    From tiny_python_projects with MIT License 5 votes vote down vote up
def test_choose():
    """Test choose"""

    state = random.getstate()
    random.seed(1)
    assert choose('a') == 'a'
    assert choose('b') == 'b'
    assert choose('c') == 'C'
    assert choose('d') == 'd'
    random.setstate(state)


# -------------------------------------------------- 
Example #29
Source File: util_lstm_seqlabel.py    From neural_wfst with MIT License 5 votes vote down vote up
def shuffle(lol):
    '''
    shuffle inplace each list in the same order by ensuring that we
    use the same state for every run of shuffle.

    lol :: list of list as input
    '''
    state = random.getstate()
    for l in lol:
        random.setstate(state)
        random.shuffle(l) 
Example #30
Source File: solution4_list_comprehension.py    From tiny_python_projects with MIT License 5 votes vote down vote up
def test_choose():
    """Test choose"""

    state = random.getstate()
    random.seed(1)
    assert choose('a') == 'a'
    assert choose('b') == 'b'
    assert choose('c') == 'C'
    assert choose('d') == 'd'
    random.setstate(state)


# --------------------------------------------------