cryptorandom module

class SHA256(seed=None)[source]

PRNG based on the SHA-256 cryptographic hash function.

Methods

betavariate(alpha, beta)

Beta distribution.

choice(seq)

Choose a random element from a non-empty sequence.

choices(population[, weights, cum_weights, k])

Return a k sized list of population elements chosen with replacement.

expovariate(lambd)

Exponential distribution.

gammavariate(alpha, beta)

Gamma distribution.

gauss(mu, sigma)

Gaussian distribution.

getrandbits(k)

Generate k pseudorandom bits.

getstate()

Get the current state of the PRNG

jumpahead(n)

Jump ahead n steps in the period

lognormvariate(mu, sigma)

Log normal distribution.

next()

Increment the counter and basehash by one

nextRandom()

Generate the next hash value

normalvariate(mu, sigma)

Normal distribution.

paretovariate(alpha)

Pareto distribution.

randbelow_from_randbits(n)

Generate a random integer between 0 (inclusive) and n (exclusive).

randbytes(n)

Generate n random bytes.

randint(a, b[, size])

Generate random integers between a (inclusive) and b (exclusive).

randint_trunc(a, b[, size])

Deprecated.

random([size])

Generate random numbers between 0 and 1.

randrange(start[, stop, step])

Choose a random item from range(start, stop[, step]).

sample(population, k, *[, counts])

Chooses k unique random elements from a population sequence or set.

seed([baseseed])

Initialize internal seed and hashable object with counter 0.

setstate([baseseed, counter, randbits_remaining])

Set the state (seed and counter)

shuffle(x[, random])

Shuffle list x in place, and return None.

triangular([low, high, mode])

Triangular distribution.

uniform(a, b)

Get a random number in the range [a, b) or [a, b] depending on rounding.

vonmisesvariate(mu, kappa)

Circular data distribution.

weibullvariate(alpha, beta)

Weibull distribution.

getrandbits(k)[source]

Generate k pseudorandom bits.

If self.randbits contains at least k bits, returns k of those bits and removes them. If self.randbits has fewer than k bits, calls self.nextRandom() as many times as needed to populate self.randbits with at least k random bits, returns those k, and keeps any remaining bits in self.randbits

Parameters
kint

number of pseudorandom bits

getstate()[source]

Get the current state of the PRNG

jumpahead(n)[source]

Jump ahead n steps in the period

>>> r = SHA256(5)
>>> r.jumpahead(5)
>>> repr(r)
'SHA256 PRNG with seed 5 and counter 5'
next()[source]

Increment the counter and basehash by one

nextRandom()[source]

Generate the next hash value

>>> r = SHA256(12345678901234567890)
>>> r.next()
>>> r.nextRandom()
4da594a8ab6064d666eab2bdf20cb4480e819e0c3102ca353de57caae1d11fd1
randbelow_from_randbits(n)[source]

Generate a random integer between 0 (inclusive) and n (exclusive). Raises ValueError if n==0.

Parameters
nint

upper limit

randint(a, b, size=None)[source]

Generate random integers between a (inclusive) and b (exclusive). size controls the number of ints generated. If size=None, just one is produced.

>>> r = SHA256(12345678901234567890)
>>> r.randint(0, 5, size=3)
array([3, 2, 4])
Parameters
aint

lower limit (included in samples)

bint

upper limit (not included in samples)

size{int, tuple, None}

If None (default), return a single random number. If size is an int, return that many random numbers. If size is a tuple, it determines the shape of an array filled with random numbers.

randint_trunc(a, b, size=None)[source]

Deprecated. For large values of (b-a), this algorithm does not produce integers uniformly at random.

Generate random integers between a (inclusive) and b (exclusive). size controls the number of ints generated. If size=None, just one is produced.

>>> r = SHA256(12345678901234567890)
>>> r.randint_trunc(0, 5, size=3)
array([0, 0, 0])
Parameters
aint

lower limit (included in samples)

bint

upper limit (not included in samples)

size{int, tuple, None}

If None (default), return a single random number. If size is an int, return that many random numbers. If size is a tuple, it determines the shape of an array filled with random numbers.

random(size=None)[source]

Generate random numbers between 0 and 1. size controls the number of ints generated. If size=None, just one is produced. The following tests match the output of Ron’s and Philip’s implementations.

>>> r = SHA256(12345678901234567890)
>>> r.random(2)
array([0.9272915426537484, 0.1916135318809483], dtype=object)
>>> r.random((2, 2))
array([[0.5846237047310486, 0.18694233108130068],
       [0.9022661737961881, 0.052310932788987144]], dtype=object)
Parameters
size{int, tuple, None}

If None (default), return a single random number. If size is an int, return that many random numbers. If size is a tuple, it determines the shape of an array filled with random numbers.

seed(baseseed=None)[source]

Initialize internal seed and hashable object with counter 0.

Parameters
baseseed{None, int, string} (optional)

Random seed used to initialize the PRNG. It can be an integer of arbitrary length, a string of arbitrary length, or None. Default is None.

counterint (optional)
Integer that counts how many times the PRNG has been called. The counter

is used to update the internal state after each step. Default is 0.

setstate(baseseed=None, counter=0, randbits_remaining=0)[source]

Set the state (seed and counter)

Parameters
baseseed{None, int, string} (optional)

Random seed used to initialize the PRNG. It can be an integer of arbitrary length, a string of arbitrary length, or None. Default is None.

counterint (optional)
Integer that counts how many times the PRNG has been called. The counter

is used to update the internal state after each step. Default is 0.