On this page
secrets.SystemRandom
Prev: secrets.randbits | Up: secrets | Next: secrets.token_bytes
Rel: secrets.choice, secrets.compare_digest, secrets.randbelow, secrets.randbits
Ref: docs.python.org — secrets.SystemRandom
Tags: #public #python3 #stdlib #secrets
Import
import secrets
from secrets import SystemRandom
Signature & Overview
secrets.SystemRandom(x=None)
Alternate random number generator using sources provided by the operating system (such as /dev/urandom on Unix or CryptGenRandom on Windows).
Methods & Attributes
SystemRandom.betavariate(self, alpha, beta)— Beta distribution.SystemRandom.binomialvariate(self, n=1, p=0.5)— Binomial random variable.SystemRandom.choice(self, seq)— Choose a random element from a non-empty sequence.SystemRandom.choices(self, population, weights=None, *, cum_weights=None, k=1)— Return a k sized list of population elements chosen with replacement.SystemRandom.expovariate(self, lambd=1.0)— Exponential distribution.SystemRandom.gammavariate(self, alpha, beta)— Gamma distribution. Not the gamma function! Conditions on the parameters are alpha > 0 and beta > 0.SystemRandom.gauss(self, mu=0.0, sigma=1.0)— Gaussian distribution.SystemRandom.getrandbits(self, k)— getrandbits(k) -> x. Generates an int with k random bits.SystemRandom.getstate(self, *args, **kwds)— Method should not be called for a system random number generator.SystemRandom.lognormvariate(self, mu, sigma)— Log normal distribution.SystemRandom.normalvariate(self, mu=0.0, sigma=1.0)— Normal distribution.SystemRandom.paretovariate(self, alpha)— Pareto distribution. alpha is the shape parameter.SystemRandom.randbytes(self, n)— Generate n random bytes.SystemRandom.randint(self, a, b)— Return random integer in range [a, b], including both end points.SystemRandom.random(self)— Get the next random number in the range 0.0 <= X < 1.0.SystemRandom.randrange(self, start, stop=None, step=1)— Choose a random item from range(stop) or range(start, stop[, step]).SystemRandom.sample(self, population, k, *, counts=None)— Chooses k unique random elements from a population sequence.SystemRandom.seed(self, *args, **kwds)— Stub method. Not used for a system random number generator.SystemRandom.setstate(self, *args, **kwds)— Method should not be called for a system random number generator.SystemRandom.shuffle(self, x)— Shuffle list x in place, and return None.SystemRandom.triangular(self, low=0.0, high=1.0, mode=None)— Triangular distribution.SystemRandom.uniform(self, a, b)— Get a random number in the range [a, b) or [a, b] depending on rounding.SystemRandom.vonmisesvariate(self, mu, kappa)— Circular data distribution.SystemRandom.weibullvariate(self, alpha, beta)— Weibull distribution.
Usage
import secrets
rng = secrets.SystemRandom()
items = [1, 2, 3, 4]
rng.shuffle(items)
assert sorted(items) == [1, 2, 3, 4]
Gotchas
- CSPRNG-backed
random.Randominterface: Usesecrets.SystemRandom()whenever you need.shuffle(),.sample(), or.choices()with cryptographic randomness. - Cannot be seeded: Calling
rng.seed(...)is a no-op because entropy always comes from the OS.
REPL Inspect
from secrets import SystemRandom
help(SystemRandom)
Linked from
Related notes
- tuts/python3/stdlib_module/secrets/choice Linked here; links to this page; shared tags: #secrets, #stdlib
- tuts/python3/stdlib_module/secrets/compare_digest Linked here; links to this page; shared tags: #secrets, #stdlib
- tuts/python3/stdlib_module/secrets/randbelow Linked here; links to this page; shared tags: #secrets, #stdlib
- tuts/python3/stdlib_module/secrets/randbits Linked here; links to this page; shared tags: #secrets, #stdlib
- tuts/python3/stdlib_module/secrets/token_bytes Linked here; links to this page; shared tags: #secrets, #stdlib
- tuts/python3/stdlib_module/secrets Linked here; links to this page; shared tags: #stdlib
- tuts/python3/stdlib_module/secrets/token_hex shared tags: #secrets, #stdlib
- tuts/python3/stdlib_module/secrets/token_urlsafe shared tags: #secrets, #stdlib
- tuts/c/stdlib shared tags: #stdlib
- tuts/c/stdlib/assert shared tags: #stdlib