Python random.Random() Examples

The following are code examples for showing how to use random.Random(). They are from open source Python projects. You can vote up the examples you like or vote down the ones you don't like.

Example 1
Project: pyblish-win   Author: pyblish   File: test_random.py    GNU Lesser General Public License v3.0 6 votes vote down vote up
def test_zeroinputs(self):
        # Verify that distributions can handle a series of zero inputs'
        g = random.Random()
        x = [g.random() for i in xrange(50)] + [0.0]*5
        g.random = x[:].pop; g.uniform(1,10)
        g.random = x[:].pop; g.paretovariate(1.0)
        g.random = x[:].pop; g.expovariate(1.0)
        g.random = x[:].pop; g.weibullvariate(1.0, 1.0)
        g.random = x[:].pop; g.vonmisesvariate(1.0, 1.0)
        g.random = x[:].pop; g.normalvariate(0.0, 1.0)
        g.random = x[:].pop; g.gauss(0.0, 1.0)
        g.random = x[:].pop; g.lognormvariate(0.0, 1.0)
        g.random = x[:].pop; g.vonmisesvariate(0.0, 1.0)
        g.random = x[:].pop; g.gammavariate(0.01, 1.0)
        g.random = x[:].pop; g.gammavariate(1.0, 1.0)
        g.random = x[:].pop; g.gammavariate(200.0, 1.0)
        g.random = x[:].pop; g.betavariate(3.0, 3.0)
        g.random = x[:].pop; g.triangular(0.0, 1.0, 1.0/3.0) 
Example 2
Project: pyblish-win   Author: pyblish   File: test_random.py    GNU Lesser General Public License v3.0 6 votes vote down vote up
def test_constant(self):
        g = random.Random()
        N = 100
        for variate, args, expected in [
                (g.uniform, (10.0, 10.0), 10.0),
                (g.triangular, (10.0, 10.0), 10.0),
                (g.triangular, (10.0, 10.0, 10.0), 10.0),
                (g.expovariate, (float('inf'),), 0.0),
                (g.vonmisesvariate, (3.0, float('inf')), 3.0),
                (g.gauss, (10.0, 0.0), 10.0),
                (g.lognormvariate, (0.0, 0.0), 1.0),
                (g.lognormvariate, (-float('inf'), 0.0), 0.0),
                (g.normalvariate, (10.0, 0.0), 10.0),
                (g.paretovariate, (float('inf'),), 1.0),
                (g.weibullvariate, (10.0, float('inf')), 10.0),
                (g.weibullvariate, (0.0, 10.0), 0.0),
            ]:
            for i in range(N):
                self.assertEqual(variate(*args), expected) 
Example 3
Project: DOTA_models   Author: ringringyi   File: neural_programmer.py    Apache License 2.0 6 votes vote down vote up
def __init__(self):
    global FLAGS
    self.FLAGS = FLAGS
    self.unk_token = "UNK"
    self.entry_match_token = "entry_match"
    self.column_match_token = "column_match"
    self.dummy_token = "dummy_token"
    self.tf_data_type = {}
    self.tf_data_type["double"] = tf.float64
    self.tf_data_type["float"] = tf.float32
    self.np_data_type = {}
    self.np_data_type["double"] = np.float64
    self.np_data_type["float"] = np.float32
    self.operations_set = ["count"] + [
        "prev", "next", "first_rs", "last_rs", "group_by_max", "greater",
        "lesser", "geq", "leq", "max", "min", "word-match"
    ] + ["reset_select"] + ["print"]
    self.word_ids = {}
    self.reverse_word_ids = {}
    self.word_count = {}
    self.random = Random(FLAGS.python_seed) 
Example 4
Project: Dumb-Cogs   Author: irdumbs   File: adventure.py    MIT License 6 votes vote down vote up
def __init__(self, seed=None):
        Data.__init__(self)
        self.output = ''
        self.yesno_callback = False
        self.yesno_casual = False       # whether to insist they answer

        self.clock1 = 30                # counts down from finding last treasure
        self.clock2 = 50                # counts down until cave closes
        self.is_closing = False         # is the cave closing?
        self.panic = False              # they tried to leave during closing?
        self.is_closed = False          # is the cave closed?
        self.is_done = False            # caller can check for "game over"
        self.could_fall_in_pit = False  # could the player fall into a pit?

        self.random_generator = random.Random()
        if seed is not None:
            self.random_generator.seed(seed) 
Example 5
Project: ProceduralSettlementsInMinecraft   Author: abrightmoore   File: FarmLand.py    GNU General Public License v3.0 6 votes vote down vote up
def create(generatorName,level,areas):
	log("Generating a "+generatorName)
	while len(areas) > 0:
		(box,availableFlag) = areas.pop()
		width,height,depth = getDimensionsFromBox(box)
		log("Generating a "+generatorName+" at "+str(box))
		
		MINW = width-2
		MIND = depth-2
		SEED = randint(1000000000,9999999999)
		R = Random(SEED) # Seed goes here
		firstBox = (box.minx,box.minz,box.maxx-box.minx,box.maxz-box.minz)
		h = box.maxy-box.miny
		boxes = [firstBox]
		for i in xrange(0,randint(1,5)):
			boxes = chopUpBoxes2D(R,boxes,MINW,MIND)
		
		for (x,z,w,d) in boxes:
			Farmland(level,BoundingBox((x,box.miny,z),(w,h,d))) 
Example 6
Project: xblock-leaderboard   Author: open-craft   File: mock.py    GNU Affero General Public License v3.0 6 votes vote down vote up
def get_grades(self, target_block_id, limit_hint=None):
        """
        Generate random grades for target_block_id and descendants.

        Results should be consistent for use in tests etc.
        """
        # Even though this is just a mock source, verify target_block_id and that the runtime can load blocks for us:
        self.host_block.runtime.get_block(target_block_id)
        # Seed a random generator consistent based on target_block_id:
        rand = random.Random(target_block_id)

        if limit_hint is None:
            limit_hint = rand.randint(5, 80)

        def random_name():
            return u"{first} {initial}.".format(
                first=rand.choice(self.NAMES),
                initial=rand.choice(self.ALPHABET)
            )

        def random_grade():
            return rand.randint(0, 100)

        return [(random_grade(), {"name": random_name()}) for _ in xrange(0, limit_hint)] 
Example 7
Project: razzy-spinner   Author: rafasashi   File: hmm.py    GNU General Public License v3.0 6 votes vote down vote up
def demo_bw():
    # demo Baum Welch by generating some sequences and then performing
    # unsupervised training on them

    print()
    print("Baum-Welch demo for market example")
    print()

    model, states, symbols = _market_hmm_example()

    # generate some random sequences
    training = []
    import random
    rng = random.Random()
    rng.seed(0)
    for i in range(10):
        item = model.random_sample(rng, 5)
        training.append([(i[0], None) for i in item])

    # train on those examples, starting with the model that generated them
    trainer = HiddenMarkovModelTrainer(states, symbols)
    hmm = trainer.train_unsupervised(training, model=model,
                                     max_iterations=1000) 
Example 8
Project: dbscan   Author: aroques   File: dataset.py    MIT License 6 votes vote down vote up
def generate_random_points(num_points: int, bound_for_x: List[float], bound_for_y: List[float], seed: int):
    """Generate random data.

    Args:
        num_points: The number of points to generate.
        bound_for_x: The bounds for possible values of X.
        bound_for_y: The bounds for possible values of Y.
        seed: Seed for Random.

    Returns:
        N points
    """
    r = Random(seed)
    x_min, x_max = bound_for_x
    y_min, y_max = bound_for_y
    data = []
    for _ in range(num_points):
        x = x_min + (x_max - x_min) * r.random()
        y = y_min + (y_max - y_min) * r.random()
        point = (x, y)
        data.append(point)
    return data 
Example 9
Project: OpenBottle   Author: xiaozhuchacha   File: hmm.py    MIT License 6 votes vote down vote up
def demo_bw():
    # demo Baum Welch by generating some sequences and then performing
    # unsupervised training on them

    print()
    print("Baum-Welch demo for market example")
    print()

    model, states, symbols = _market_hmm_example()

    # generate some random sequences
    training = []
    import random
    rng = random.Random()
    rng.seed(0)
    for i in range(10):
        item = model.random_sample(rng, 5)
        training.append([(i[0], None) for i in item])

    # train on those examples, starting with the model that generated them
    trainer = HiddenMarkovModelTrainer(states, symbols)
    hmm = trainer.train_unsupervised(training, model=model,
                                     max_iterations=1000) 
Example 10
Project: OpenBottle   Author: xiaozhuchacha   File: hmm.py    MIT License 6 votes vote down vote up
def demo_bw():
    # demo Baum Welch by generating some sequences and then performing
    # unsupervised training on them

    print()
    print("Baum-Welch demo for market example")
    print()

    model, states, symbols = _market_hmm_example()

    # generate some random sequences
    training = []
    import random
    rng = random.Random()
    rng.seed(0)
    for i in range(10):
        item = model.random_sample(rng, 5)
        training.append([(i[0], None) for i in item])

    # train on those examples, starting with the model that generated them
    trainer = HiddenMarkovModelTrainer(states, symbols)
    hmm = trainer.train_unsupervised(training, model=model,
                                     max_iterations=1000) 
Example 11
Project: NiujiaoDebugger   Author: MrSrc   File: test_tarfile.py    GNU General Public License v3.0 6 votes vote down vote up
def test_ignore_zeros(self):
        # Test TarFile's ignore_zeros option.
        # generate 512 pseudorandom bytes
        data = Random(0).getrandbits(512*8).to_bytes(512, 'big')
        for char in (b'\0', b'a'):
            # Test if EOFHeaderError ('\0') and InvalidHeaderError ('a')
            # are ignored correctly.
            with self.open(tmpname, "w") as fobj:
                fobj.write(char * 1024)
                tarinfo = tarfile.TarInfo("foo")
                tarinfo.size = len(data)
                fobj.write(tarinfo.tobuf())
                fobj.write(data)

            tar = tarfile.open(tmpname, mode="r", ignore_zeros=True)
            try:
                self.assertListEqual(tar.getnames(), ["foo"],
                    "ignore_zeros=True should have skipped the %r-blocks" %
                    char)
            finally:
                tar.close() 
Example 12
Project: NiujiaoDebugger   Author: MrSrc   File: test_random.py    GNU General Public License v3.0 6 votes vote down vote up
def test_zeroinputs(self):
        # Verify that distributions can handle a series of zero inputs'
        g = random.Random()
        x = [g.random() for i in range(50)] + [0.0]*5
        g.random = x[:].pop; g.uniform(1,10)
        g.random = x[:].pop; g.paretovariate(1.0)
        g.random = x[:].pop; g.expovariate(1.0)
        g.random = x[:].pop; g.weibullvariate(1.0, 1.0)
        g.random = x[:].pop; g.vonmisesvariate(1.0, 1.0)
        g.random = x[:].pop; g.normalvariate(0.0, 1.0)
        g.random = x[:].pop; g.gauss(0.0, 1.0)
        g.random = x[:].pop; g.lognormvariate(0.0, 1.0)
        g.random = x[:].pop; g.vonmisesvariate(0.0, 1.0)
        g.random = x[:].pop; g.gammavariate(0.01, 1.0)
        g.random = x[:].pop; g.gammavariate(1.0, 1.0)
        g.random = x[:].pop; g.gammavariate(200.0, 1.0)
        g.random = x[:].pop; g.betavariate(3.0, 3.0)
        g.random = x[:].pop; g.triangular(0.0, 1.0, 1.0/3.0) 
Example 13
Project: NiujiaoDebugger   Author: MrSrc   File: test_random.py    GNU General Public License v3.0 6 votes vote down vote up
def test_constant(self):
        g = random.Random()
        N = 100
        for variate, args, expected in [
                (g.uniform, (10.0, 10.0), 10.0),
                (g.triangular, (10.0, 10.0), 10.0),
                (g.triangular, (10.0, 10.0, 10.0), 10.0),
                (g.expovariate, (float('inf'),), 0.0),
                (g.vonmisesvariate, (3.0, float('inf')), 3.0),
                (g.gauss, (10.0, 0.0), 10.0),
                (g.lognormvariate, (0.0, 0.0), 1.0),
                (g.lognormvariate, (-float('inf'), 0.0), 0.0),
                (g.normalvariate, (10.0, 0.0), 10.0),
                (g.paretovariate, (float('inf'),), 1.0),
                (g.weibullvariate, (10.0, float('inf')), 10.0),
                (g.weibullvariate, (0.0, 10.0), 0.0),
            ]:
            for i in range(N):
                self.assertEqual(variate(*args), expected) 
Example 14
Project: NiujiaoDebugger   Author: MrSrc   File: test_random.py    GNU General Public License v3.0 6 votes vote down vote up
def test_after_fork(self):
        # Test the global Random instance gets reseeded in child
        r, w = os.pipe()
        pid = os.fork()
        if pid == 0:
            # child process
            try:
                val = random.getrandbits(128)
                with open(w, "w") as f:
                    f.write(str(val))
            finally:
                os._exit(0)
        else:
            # parent process
            os.close(w)
            val = random.getrandbits(128)
            with open(r, "r") as f:
                child_val = eval(f.read())
            self.assertNotEqual(val, child_val)

            pid, status = os.waitpid(pid, 0)
            self.assertEqual(status, 0) 
Example 15
Project: NiujiaoDebugger   Author: MrSrc   File: stringbench.py    GNU General Public License v3.0 6 votes vote down vote up
def _make_2000_lines():
    import random
    r = random.Random(100)
    chars = list(map(chr, range(32, 128)))
    i = 0
    while i < len(chars):
        chars[i] = " "
        i += r.randrange(9)
    s = "".join(chars)
    s = s*4
    words = []
    for i in range(2000):
        start = r.randrange(96)
        n = r.randint(5, 65)
        words.append(s[start:start+n])
    return "\n".join(words)+"\n" 
Example 16
Project: 0xbtc-discord-price-bot   Author: 0x1d00ffff   File: all_self_tests.py    MIT License 6 votes vote down vote up
def test_fuzzing_prettify_decimals(self):
        from formatting_helpers import prettify_decimals
        import random
        iterations = 100000  # 4 seconds on an i7-5700HQ
        min_value, max_value = -1e30, 1e30
        # fixed seed so randomness is repeatable
        myrandom = random.Random("myseed")
        # test formatting integers
        for _ in range(iterations):
            number = myrandom.randint(min_value, max_value)
            with self.subTest(number=number):
                formatted = prettify_decimals(number)
                self.assertTrue(isinstance(formatted, str))
                self.assertTrue(len(formatted) <= 16)
        # test formatting floats
        for _ in range(iterations):
            number = myrandom.uniform(min_value, max_value)
            with self.subTest(number=number):
                formatted = prettify_decimals(number)
                self.assertTrue(isinstance(formatted, str))
                self.assertTrue(len(formatted) <= 16) 
Example 17
Project: Health-Checker   Author: KriAga   File: hmm.py    MIT License 6 votes vote down vote up
def demo_bw():
    # demo Baum Welch by generating some sequences and then performing
    # unsupervised training on them

    print()
    print("Baum-Welch demo for market example")
    print()

    model, states, symbols = _market_hmm_example()

    # generate some random sequences
    training = []
    import random
    rng = random.Random()
    rng.seed(0)
    for i in range(10):
        item = model.random_sample(rng, 5)
        training.append([(i[0], None) for i in item])

    # train on those examples, starting with the model that generated them
    trainer = HiddenMarkovModelTrainer(states, symbols)
    hmm = trainer.train_unsupervised(training, model=model,
                                     max_iterations=1000) 
Example 18
Project: BERT-Classification-Tutorial   Author: Socialbird-AILab   File: modeling_test.py    Apache License 2.0 5 votes vote down vote up
def ids_tensor(cls, shape, vocab_size, rng=None, name=None):
        """Creates a random int32 tensor of the shape within the vocab size."""
        if rng is None:
            rng = random.Random()

        total_dims = 1
        for dim in shape:
            total_dims *= dim

        values = []
        for _ in range(total_dims):
            values.append(rng.randint(0, vocab_size - 1))

        return tf.constant(value=values, dtype=tf.int32, shape=shape, name=name) 
Example 19
Project: pyblish-win   Author: pyblish   File: tempfile.py    GNU Lesser General Public License v3.0 5 votes vote down vote up
def rng(self):
        cur_pid = _os.getpid()
        if cur_pid != getattr(self, '_rng_pid', None):
            self._rng = _Random()
            self._rng_pid = cur_pid
        return self._rng 
Example 20
Project: pyblish-win   Author: pyblish   File: test_zlib.py    GNU Lesser General Public License v3.0 5 votes vote down vote up
def test_odd_flush(self):
        # Test for odd flushing bugs noted in 2.0, and hopefully fixed in 2.1
        import random
        # Testing on 17K of "random" data

        # Create compressor and decompressor objects
        co = zlib.compressobj(zlib.Z_BEST_COMPRESSION)
        dco = zlib.decompressobj()

        # Try 17K of data
        # generate random data stream
        try:
            # In 2.3 and later, WichmannHill is the RNG of the bug report
            gen = random.WichmannHill()
        except AttributeError:
            try:
                # 2.2 called it Random
                gen = random.Random()
            except AttributeError:
                # others might simply have a single RNG
                gen = random
        gen.seed(1)
        data = genblock(1, 17 * 1024, generator=gen)

        # compress, sync-flush, and decompress
        first = co.compress(data)
        second = co.flush(zlib.Z_SYNC_FLUSH)
        expanded = dco.decompress(first + second)

        # if decompressed data is different from the input data, choke.
        self.assertEqual(expanded, data, "17K random source doesn't match") 
Example 21
Project: pyblish-win   Author: pyblish   File: test_random.py    GNU Lesser General Public License v3.0 5 votes vote down vote up
def test_avg_std(self):
        # Use integration to test distribution average and standard deviation.
        # Only works for distributions which do not consume variates in pairs
        g = random.Random()
        N = 5000
        x = [i/float(N) for i in xrange(1,N)]
        for variate, args, mu, sigmasqrd in [
                (g.uniform, (1.0,10.0), (10.0+1.0)/2, (10.0-1.0)**2/12),
                (g.triangular, (0.0, 1.0, 1.0/3.0), 4.0/9.0, 7.0/9.0/18.0),
                (g.expovariate, (1.5,), 1/1.5, 1/1.5**2),
                (g.vonmisesvariate, (1.23, 0), pi, pi**2/3),
                (g.paretovariate, (5.0,), 5.0/(5.0-1),
                                  5.0/((5.0-1)**2*(5.0-2))),
                (g.weibullvariate, (1.0, 3.0), gamma(1+1/3.0),
                                  gamma(1+2/3.0)-gamma(1+1/3.0)**2) ]:
            g.random = x[:].pop
            y = []
            for i in xrange(len(x)):
                try:
                    y.append(variate(*args))
                except IndexError:
                    pass
            s1 = s2 = 0
            for e in y:
                s1 += e
                s2 += (e - mu) ** 2
            N = len(y)
            self.assertAlmostEqual(s1/N, mu, places=2,
                                   msg='%s%r' % (variate.__name__, args))
            self.assertAlmostEqual(s2/(N-1), sigmasqrd, places=2,
                                   msg='%s%r' % (variate.__name__, args)) 
Example 22
Project: pyblish-win   Author: pyblish   File: test_random.py    GNU Lesser General Public License v3.0 5 votes vote down vote up
def test_random_subclass_with_kwargs(self):
        # SF bug #1486663 -- this used to erroneously raise a TypeError
        class Subclass(random.Random):
            def __init__(self, newarg=None):
                random.Random.__init__(self)
        Subclass(newarg=1) 
Example 23
Project: flasky   Author: RoseOu   File: flask_httpauth.py    MIT License 5 votes vote down vote up
def __init__(self, scheme=None, realm=None, use_ha1_pw=False):
        super(HTTPDigestAuth, self).__init__(scheme, realm)
        self.use_ha1_pw = use_ha1_pw
        self.random = SystemRandom()
        try:
            self.random.random()
        except NotImplementedError:
            self.random = Random()

        def _generate_random():
            return md5(str(self.random.random()).encode('utf-8')).hexdigest()

        def default_generate_nonce():
            session["auth_nonce"] = _generate_random()
            return session["auth_nonce"]

        def default_verify_nonce(nonce):
            return nonce == session.get("auth_nonce")

        def default_generate_opaque():
            session["auth_opaque"] = _generate_random()
            return session["auth_opaque"]

        def default_verify_opaque(opaque):
            return opaque == session.get("auth_opaque")

        self.generate_nonce(default_generate_nonce)
        self.generate_opaque(default_generate_opaque)
        self.verify_nonce(default_verify_nonce)
        self.verify_opaque(default_verify_opaque) 
Example 24
Project: dynamic-training-with-apache-mxnet-on-aws   Author: awslabs   File: stt_datagenerator.py    Apache License 2.0 5 votes vote down vote up
def __init__(self, save_dir, model_name, step=10, window=20, max_freq=8000, desc_file=None):
        """
        Params:
            step (int): Step size in milliseconds between windows
            window (int): FFT window size in milliseconds
            max_freq (int): Only FFT bins corresponding to frequencies between
                [0, max_freq] are returned
            desc_file (str, optional): Path to a JSON-line file that contains
                labels and paths to the audio files. If this is None, then
                load metadata right away
        """
        #calc_feat_dim returns int(0.001*window*max_freq)+1
        super(DataGenerator, self).__init__()
        # feat_dim=0.001*20*8000+1=161
        self.feat_dim = calc_feat_dim(window, max_freq)
        # 1d 161 length of array filled with zeros
        self.feats_mean = np.zeros((self.feat_dim,))
        # 1d 161 length of array filled with 1s
        self.feats_std = np.ones((self.feat_dim,))
        self.max_input_length = 0
        self.max_length_list_in_batch = []
        # 1d 161 length of array filled with random value
        #[0.0, 1.0)
        self.rng = random.Random()
        if desc_file is not None:
            self.load_metadata_from_desc_file(desc_file)
        self.step = step
        self.window = window
        self.max_freq = max_freq
        self.save_dir = save_dir
        self.model_name = model_name 
Example 25
Project: sic   Author: Yanixos   File: tempfile.py    GNU General Public License v3.0 5 votes vote down vote up
def rng(self):
        cur_pid = _os.getpid()
        if cur_pid != getattr(self, '_rng_pid', None):
            self._rng = _Random()
            self._rng_pid = cur_pid
        return self._rng 
Example 26
Project: Dumb-Cogs   Author: irdumbs   File: adventure.py    MIT License 5 votes vote down vote up
def resume(self, obj):
        """Returns an Adventure game saved to the given file."""
        if isinstance(obj, str):
            savefile = open(obj, 'rb')
        else:
            savefile = obj
        game = pickle.loads(zlib.decompress(savefile.read()))
        if savefile is not obj:
            savefile.close()
        # Reinstate the random number generator.
        game.random_generator = random.Random()
        game.random_generator.setstate(game.random_state)
        del game.random_state
        return game 
Example 27
Project: mpu   Author: MartinThoma   File: datetime.py    MIT License 5 votes vote down vote up
def generate(minimum, maximum, local_random=random.Random()):
    """
    Generate a random date.

    The generated dates are uniformly distributed.

    Parameters
    ----------
    minimum : datetime object
    maximum : datetime object
    local_random : random.Random

    Returns
    -------
    generated_date : datetime object

    Examples
    --------
    >>> import random; r = random.Random(); r.seed(0)
    >>> from datetime import datetime

    >>> generate(datetime(2018, 1, 1), datetime(2018, 1, 2), local_random=r)
    datetime.datetime(2018, 1, 1, 20, 15, 58, 47972)

    >>> generate(datetime(2018, 1, 1), datetime(2018, 1, 2), local_random=r)
    datetime.datetime(2018, 1, 1, 18, 11, 27, 260414)
    """
    if not (minimum < maximum):
        raise ValueError("{} is not smaller than {}".format(minimum, maximum))

    # Python 3 allows direct multiplication of timedelta with a float, but
    # Python 2.7 does not. Hence this work-around.
    time_d = maximum - minimum
    time_d_float = time_d.total_seconds()
    time_d_rand = dt.timedelta(seconds=time_d_float * local_random.random())
    generated = minimum + time_d_rand
    return generated 
Example 28
Project: bert-sts   Author: swen128   File: modeling_test.py    Apache License 2.0 5 votes vote down vote up
def ids_tensor(cls, shape, vocab_size, rng=None, name=None):
    """Creates a random int32 tensor of the shape within the vocab size."""
    if rng is None:
      rng = random.Random()

    total_dims = 1
    for dim in shape:
      total_dims *= dim

    values = []
    for _ in range(total_dims):
      values.append(rng.randint(0, vocab_size - 1))

    return tf.constant(value=values, dtype=tf.int32, shape=shape, name=name) 
Example 29
Project: bert-sts   Author: swen128   File: create_pretraining_data.py    Apache License 2.0 5 votes vote down vote up
def main(_):
  tf.logging.set_verbosity(tf.logging.INFO)

  tokenizer = tokenization.FullTokenizer(
      vocab_file=FLAGS.vocab_file, do_lower_case=FLAGS.do_lower_case)

  input_files = []
  for input_pattern in FLAGS.input_file.split(","):
    input_files.extend(tf.gfile.Glob(input_pattern))

  tf.logging.info("*** Reading from input files ***")
  for input_file in input_files:
    tf.logging.info("  %s", input_file)

  rng = random.Random(FLAGS.random_seed)
  instances = create_training_instances(
      input_files, tokenizer, FLAGS.max_seq_length, FLAGS.dupe_factor,
      FLAGS.short_seq_prob, FLAGS.masked_lm_prob, FLAGS.max_predictions_per_seq,
      rng)

  output_files = FLAGS.output_file.split(",")
  tf.logging.info("*** Writing to output files ***")
  for output_file in output_files:
    tf.logging.info("  %s", output_file)

  write_instance_to_example_files(instances, tokenizer, FLAGS.max_seq_length,
                                  FLAGS.max_predictions_per_seq, output_files) 
Example 30
Project: jawfish   Author: war-and-code   File: tempfile.py    MIT License 5 votes vote down vote up
def rng(self):
        cur_pid = _os.getpid()
        if cur_pid != getattr(self, '_rng_pid', None):
            self._rng = _Random()
            self._rng_pid = cur_pid
        return self._rng 
Example 31
Project: edit-bone-constraint   Author: HolisticCoders   File: misc.py    GNU General Public License v3.0 5 votes vote down vote up
def create_py_random_state(random_state=None):
    """Returns a random.Random instance depending on input.

    Parameters
    ----------
    random_state : int or random number generator or None (default=None)
        If int, return a random.Random instance set with seed=int.
        if random.Random instance, return it.
        if None or the `random` package, return the global random number
        generator used by `random`.
        if np.random package, return the global numpy random number
        generator wrapped in a PythonRandomInterface class.
        if np.random.RandomState instance, return it wrapped in
        PythonRandomInterface
        if a PythonRandomInterface instance, return it
    """
    import random
    try:
        import numpy as np
        if random_state is np.random:
            return PythonRandomInterface(np.random.mtrand._rand)
        if isinstance(random_state, np.random.RandomState):
            return PythonRandomInterface(random_state)
        if isinstance(random_state, PythonRandomInterface):
            return random_state
        has_numpy = True
    except ImportError:
        has_numpy = False

    if random_state is None or random_state is random:
        return random._inst
    if isinstance(random_state, random.Random):
        return random_state
    if isinstance(random_state, int):
        return random.Random(random_state)
    msg = '%r cannot be used to generate a random.Random instance'
    raise ValueError(msg % random_state)


# fixture for nose tests 
Example 32
Project: razzy-spinner   Author: rafasashi   File: hmm.py    GNU General Public License v3.0 5 votes vote down vote up
def random_sample(self, rng, length):
        """
        Randomly sample the HMM to generate a sentence of a given length. This
        samples the prior distribution then the observation distribution and
        transition distribution for each subsequent observation and state.
        This will mostly generate unintelligible garbage, but can provide some
        amusement.

        @return:        the randomly created state/observation sequence,
                        generated according to the HMM's probability
                        distributions. The SUBTOKENS have TEXT and TAG
                        properties containing the observation and state
                        respectively.
        @rtype:         list
        @param rng:     random number generator
        @type rng:      Random (or any object with a random() method)
        @param length:  desired output length
        @type length:   int
        """

        # sample the starting state and symbol prob dists
        tokens = []
        state = self._sample_probdist(self._priors, rng.random(), self._states)
        symbol = self._sample_probdist(self._outputs[state],
                                  rng.random(), self._symbols)
        tokens.append((symbol, state))

        for i in range(1, length):
            # sample the state transition and symbol prob dists
            state = self._sample_probdist(self._transitions[state],
                                     rng.random(), self._states)
            symbol = self._sample_probdist(self._outputs[state],
                                      rng.random(), self._symbols)
            tokens.append((symbol, state))

        return tokens 
Example 33
Project: razzy-spinner   Author: rafasashi   File: hmm.py    GNU General Public License v3.0 5 votes vote down vote up
def demo_bw():
    # demo Baum Welch by generating some sequences and then performing
    # unsupervised training on them

    # example taken from page 381, Huang et al
    symbols = ['up', 'down', 'unchanged']
    states = ['bull', 'bear', 'static']

    def pd(values, samples):
        d = {}
        for value, item in zip(values, samples):
            d[item] = value
        return DictionaryProbDist(d)

    def cpd(array, conditions, samples):
        d = {}
        for values, condition in zip(array, conditions):
            d[condition] = pd(values, samples)
        return DictionaryConditionalProbDist(d)

    A = array([[0.6, 0.2, 0.2], [0.5, 0.3, 0.2], [0.4, 0.1, 0.5]], float64)
    A = cpd(A, states, states)
    B = array([[0.7, 0.1, 0.2], [0.1, 0.6, 0.3], [0.3, 0.3, 0.4]], float64)
    B = cpd(B, states, symbols)
    pi = array([0.5, 0.2, 0.3], float64)
    pi = pd(pi, states)

    model = HiddenMarkovModel(symbols=symbols, states=states,
                              transitions=A, outputs=B, priors=pi)

    # generate some random sequences
    training = []
    import random
    rng = random.Random()
    for i in range(10):
        item = model.random_sample(rng, 5)
        training.append([(i[0], None) for i in item])

    # train on those examples, starting with the model that generated them
    trainer = HiddenMarkovModelTrainer(states, symbols)
    hmm = trainer.train_unsupervised(training, model=model, max_iterations=1000) 
Example 34
Project: razzy-spinner   Author: rafasashi   File: hmm.py    GNU General Public License v3.0 5 votes vote down vote up
def random_sample(self, rng, length):
        """
        Randomly sample the HMM to generate a sentence of a given length. This
        samples the prior distribution then the observation distribution and
        transition distribution for each subsequent observation and state.
        This will mostly generate unintelligible garbage, but can provide some
        amusement.

        :return:        the randomly created state/observation sequence,
                        generated according to the HMM's probability
                        distributions. The SUBTOKENS have TEXT and TAG
                        properties containing the observation and state
                        respectively.
        :rtype:         list
        :param rng:     random number generator
        :type rng:      Random (or any object with a random() method)
        :param length:  desired output length
        :type length:   int
        """

        # sample the starting state and symbol prob dists
        tokens = []
        state = self._sample_probdist(self._priors, rng.random(), self._states)
        symbol = self._sample_probdist(self._outputs[state],
                                  rng.random(), self._symbols)
        tokens.append((symbol, state))

        for i in range(1, length):
            # sample the state transition and symbol prob dists
            state = self._sample_probdist(self._transitions[state],
                                     rng.random(), self._states)
            symbol = self._sample_probdist(self._outputs[state],
                                      rng.random(), self._symbols)
            tokens.append((symbol, state))

        return tokens 
Example 35
Project: razzy-spinner   Author: rafasashi   File: kmeans.py    GNU General Public License v3.0 5 votes vote down vote up
def __init__(self, num_means, distance, repeats=1,
                       conv_test=1e-6, initial_means=None,
                       normalise=False, svd_dimensions=None,
                       rng=None, avoid_empty_clusters=False):

        """
        :param  num_means:  the number of means to use (may use fewer)
        :type   num_means:  int
        :param  distance:   measure of distance between two vectors
        :type   distance:   function taking two vectors and returing a float
        :param  repeats:    number of randomised clustering trials to use
        :type   repeats:    int
        :param  conv_test:  maximum variation in mean differences before
                            deemed convergent
        :type   conv_test:  number
        :param  initial_means: set of k initial means
        :type   initial_means: sequence of vectors
        :param  normalise:  should vectors be normalised to length 1
        :type   normalise:  boolean
        :param svd_dimensions: number of dimensions to use in reducing vector
                               dimensionsionality with SVD
        :type svd_dimensions: int
        :param  rng:        random number generator (or None)
        :type   rng:        Random
        :param avoid_empty_clusters: include current centroid in computation
                                     of next one; avoids undefined behavior
                                     when clusters become empty
        :type avoid_empty_clusters: boolean
        """
        VectorSpaceClusterer.__init__(self, normalise, svd_dimensions)
        self._num_means = num_means
        self._distance = distance
        self._max_difference = conv_test
        assert not initial_means or len(initial_means) == num_means
        self._means = initial_means
        assert repeats >= 1
        assert not (initial_means and repeats > 1)
        self._repeats = repeats
        self._rng = (rng if rng else random.Random())
        self._avoid_empty_clusters = avoid_empty_clusters 
Example 36
Project: zun   Author: openstack   File: name_generator.py    Apache License 2.0 5 votes vote down vote up
def __init__(self):
        self.random = Random() 
Example 37
Project: robot-navigation   Author: ronaldahmed   File: dictionary.py    MIT License 5 votes vote down vote up
def __init__(self, dictionary, annealing_schedule, 
                 stemmer=None, has_pos=True, style='bag', seed=None):
        LeskWordSenseTagger.__init__(self, 0, dictionary,
                                     stemmer, has_pos, style)
        self._annealing_schedule = annealing_schedule
        self._random = random.Random(seed) 
Example 38
Project: PAN   Author: WANG-Chaoyue   File: rng.py    MIT License 5 votes vote down vote up
def set_seed(n):
    global seed, py_rng, np_rng, t_rng
    
    seed = n
    py_rng = Random(seed)
    np_rng = RandomState(seed)
    t_rng = RandomStreams(seed) 
Example 39
Project: recipes-py   Author: luci   File: api.py    Apache License 2.0 5 votes vote down vote up
def __init__(self, module_properties, **kwargs):
    super(RandomApi, self).__init__(**kwargs)
    self._random = random.Random(
      module_properties.get(
        'seed', 1234 if self._test_data.enabled else None)) 
Example 40
Project: recipes-py   Author: luci   File: api.py    Apache License 2.0 5 votes vote down vote up
def __getattr__(self, name):
    """Access a member of `random.Random`."""
    return getattr(self._random, name) 
Example 41
Project: dbscan   Author: aroques   File: dataviz.py    MIT License 5 votes vote down vote up
def get_markers(num_markers):
    random = Random(3)
    markers = ['*', 'o', '^', '+', 'x']
    markers = random.sample(population=markers, k=num_markers)
    return markers 
Example 42
Project: dbscan   Author: aroques   File: dataviz.py    MIT License 5 votes vote down vote up
def get_palette(num_colors):
    random = Random(3)
    colors = ['blue', 'darkgreen', 'purple', 'red']
    colors = random.sample(population=colors, k=num_colors)
    return colors 
Example 43
Project: raspiblitz   Author: rootzoll   File: memo.py    MIT License 5 votes vote down vote up
def adjective_noun_pair():
    """
    taken from https://github.com/aaronbassett/Pass-phrase/blob/master/pass_phrase.py

    The MIT License (MIT)
    Copyright (c) 2012 Aaron Bassett, http://aaronbassett.com
    Permission is hereby granted, free of charge, to any person 
    obtaining a copy of this software and associated documentation 
    files (the "Software"), to deal in the Software without restriction, 
    including without limitation the rights to use, copy, modify, 
    merge, publish, distribute, sublicense, and/or sell copies of the 
    Software, and to permit persons to whom the Software is furnished 
    to do so, subject to the following conditions:
    The above copyright notice and this permission notice shall be 
    included in all copies or substantial portions of the Software.
    THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, 
    EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES 
    OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND 
    NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT 
    HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER 
    IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR 
    IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
    """

    # random.SystemRandom() should be cryptographically secure
    try:
        rng = random.SystemRandom
    except AttributeError:
        sys.stderr.write("WARNING: System does not support cryptographically "
                         "secure random number generator or you are using Python "
                         "version < 2.4.\n"
                         "Continuing with less-secure generator.\n")
        rng = random.Random

    adjective = rng().choice(adjectives)
    noun = rng().choice(nouns)
    return adjective, noun 
Example 44
Project: DRCOG_Urbansim   Author: apdjustino   File: synthesizer_create_persons_input_table.py    GNU Affero General Public License v3.0 5 votes vote down vote up
def get_random_index_name():
    letters = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz'
    return 'indx_' + ''.join(Random().sample(letters, 8)) 
Example 45
Project: lattice   Author: tensorflow   File: separately_calibrated_rtl.py    Apache License 2.0 5 votes vote down vote up
def _create_structure(self, input_dim, num_lattices, lattice_rank, rtl_seed):
    """Create and save rtl structure to model_dir."""
    rtl_random = random.Random(rtl_seed)
    structure = []
    for _ in range(num_lattices):
      structure.append(
          rtl_random.sample(six.moves.xrange(input_dim), lattice_rank))
    return structure 
Example 46
Project: lattice   Author: tensorflow   File: calibrated_rtl.py    Apache License 2.0 5 votes vote down vote up
def _create_structure(self, input_dim, num_lattices, lattice_rank, rtl_seed):
    """Create and save rtl structure to model_dir."""
    rtl_random = random.Random(rtl_seed)
    structure = []
    for _ in range(num_lattices):
      structure.append(
          rtl_random.sample(six.moves.xrange(input_dim), lattice_rank))
    return structure 
Example 47
Project: bot   Author: python-discord   File: superstarify.py    MIT License 5 votes vote down vote up
def get_nick(infraction_id: int, member_id: int) -> str:
        """Randomly select a nickname from the Superstarify nickname list."""
        log.trace(f"Choosing a random nickname for superstar #{infraction_id}.")

        rng = random.Random(str(infraction_id) + str(member_id))
        return rng.choice(STAR_NAMES)

    # This cannot be static (must have a __func__ attribute). 
Example 48
Project: link-prediction_with_deep-learning   Author: cambridgeltl   File: graph.py    MIT License 5 votes vote down vote up
def random_walk(self, path_length, alpha=0, rand=random.Random(), start=None):
    """ Returns a truncated random walk.

        path_length: Length of the random walk.
        alpha: probability of restarts.
        start: the start node of the random walk.
    """
    G = self
    if start:
      path = [start]
    else:
      # Sampling is uniform w.r.t V, and not w.r.t E
      path = [rand.choice(G.keys())]

    while len(path) < path_length:
      cur = path[-1]
      if len(G[cur]) > 0:
        if rand.random() >= alpha:
          path.append(rand.choice(G[cur]))
        else:
          path.append(path[0])
      else:
        break
    return path

# TODO add build_walks in here 
Example 49
Project: link-prediction_with_deep-learning   Author: cambridgeltl   File: graph.py    MIT License 5 votes vote down vote up
def build_deepwalk_corpus(G, num_paths, path_length, alpha=0,
                      rand=random.Random(0)):
  walks = []

  nodes = list(G.nodes())
  
  for cnt in range(num_paths):
    rand.shuffle(nodes)
    for node in nodes:
      walks.append(G.random_walk(path_length, rand=rand, alpha=alpha, start=node))
  
  return walks 
Example 50
Project: link-prediction_with_deep-learning   Author: cambridgeltl   File: graph.py    MIT License 5 votes vote down vote up
def build_deepwalk_corpus_iter(G, num_paths, path_length, alpha=0,
                      rand=random.Random(0)):
  walks = []

  nodes = list(G.nodes())

  for cnt in range(num_paths):
    rand.shuffle(nodes)
    for node in nodes:
      yield G.random_walk(path_length, rand=rand, alpha=alpha, start=node)