On this page
random.Random
Prev: random.random | Up: random | Next: random.randrange
Rel: random.betavariate, random.binomialvariate, random.choice, random.choices
Ref: docs.python.org — random.Random
Tags: #public #python3 #stdlib #random
Import
import random
from random import Random
Signature & Overview
random.Random(x=None)
Random number generator base class used by bound module functions.
Methods & Attributes
Random.betavariate(self, alpha, beta)— Beta distribution.Random.binomialvariate(self, n=1, p=0.5)— Binomial random variable.Random.choice(self, seq)— Choose a random element from a non-empty sequence.Random.choices(self, population, weights=None, *, cum_weights=None, k=1)— Return a k sized list of population elements chosen with replacement.Random.expovariate(self, lambd=1.0)— Exponential distribution.Random.gammavariate(self, alpha, beta)— Gamma distribution. Not the gamma function! Conditions on the parameters are alpha > 0 and beta > 0.Random.gauss(self, mu=0.0, sigma=1.0)— Gaussian distribution.Random.getrandbits(self, k, /)— getrandbits(k) -> x. Generates an int with k random bits.Random.getstate(self)— Return internal state; can be passed to setstate() later.Random.lognormvariate(self, mu, sigma)— Log normal distribution.Random.normalvariate(self, mu=0.0, sigma=1.0)— Normal distribution.Random.paretovariate(self, alpha)— Pareto distribution. alpha is the shape parameter.Random.randbytes(self, n)— Generate n random bytes.Random.randint(self, a, b)— Return random integer in range [a, b], including both end points.Random.random(self, /)— random() -> x in the interval [0, 1).Random.randrange(self, start, stop=None, step=1)— Choose a random item from range(stop) or range(start, stop[, step]).Random.sample(self, population, k, *, counts=None)— Chooses k unique random elements from a population sequence.Random.seed(self, a=None, version=2)— Initialize internal state from a seed.Random.setstate(self, state)— Restore internal state from object returned by getstate().Random.shuffle(self, x)— Shuffle list x in place, and return None.Random.triangular(self, low=0.0, high=1.0, mode=None)— Triangular distribution.Random.uniform(self, a, b)— Get a random number in the range [a, b) or [a, b] depending on rounding.Random.vonmisesvariate(self, mu, kappa)— Circular data distribution.Random.weibullvariate(self, alpha, beta)— Weibull distribution.
Usage
import random
rng = random.Random(42)
assert rng.randint(1, 10) == random.Random(42).randint(1, 10)
Gotchas
- Prefer isolated
random.Random(seed)instances overrandom.seed(): Keeps simulations and tests reproducible without interfering with other modules sharing global state. - Not cryptographically secure: Use
secrets/SystemRandomfor tokens or keys.
REPL Inspect
from random import Random
help(Random)
Linked from
Related notes
- tuts/python3/stdlib_module/random/random Linked here; links to this page; shared tags: #random, #stdlib
- tuts/python3/stdlib_module/random/randrange Linked here; links to this page; shared tags: #random, #stdlib
- tuts/python3/stdlib_module/random/betavariate Linked here; shared tags: #random, #stdlib
- tuts/python3/stdlib_module/random/binomialvariate Linked here; shared tags: #random, #stdlib
- tuts/python3/stdlib_module/random/choice Linked here; shared tags: #random, #stdlib
- tuts/python3/stdlib_module/random/choices Linked here; shared tags: #random, #stdlib
- tuts/python3/stdlib_module/random links to this page; shared tags: #stdlib
- tuts/python3/stdlib_module/random/expovariate shared tags: #random, #stdlib
- tuts/python3/stdlib_module/random/gammavariate shared tags: #random, #stdlib
- tuts/python3/stdlib_module/random/gauss shared tags: #random, #stdlib