💾 Archived View for tris.fyi › pydoc › random captured on 2023-04-26 at 13:31:37. Gemini links have been rewritten to link to archived content
⬅️ Previous capture (2022-01-08)
-=-=-=-=-=-=-
Random variable generators. bytes ----- uniform bytes (values between 0 and 255) integers -------- uniform within range sequences --------- pick random element pick random sample pick weighted random sample generate random permutation distributions on the real line: ------------------------------ uniform triangular normal (Gaussian) lognormal negative exponential gamma beta pareto Weibull distributions on the circle (angles 0 to 2pi) --------------------------------------------- circular uniform von Mises General notes on the underlying Mersenne Twister core generator:
Random number generator base class used by bound module functions. Used to instantiate instances of Random to get generators that don't share state. Class Random can also be subclassed if you want to use a different basic generator of your own devising: in that case, override the following methods: random(), seed(), getstate(), and setstate(). Optionally, implement a getrandbits() method so that randrange() can cover arbitrarily large ranges.
betavariate(self, alpha, beta) Beta distribution. Conditions on the parameters are alpha > 0 and beta > 0. Returned values range between 0 and 1.
choice(self, seq) Choose a random element from a non-empty sequence.
choices(self, population, weights=None, *, cum_weights=None, k=1) Return a k sized list of population elements chosen with replacement. If the relative weights or cumulative weights are not specified, the selections are made with equal probability.
expovariate(self, lambd) Exponential distribution. lambd is 1.0 divided by the desired mean. It should be nonzero. (The parameter would be called "lambda", but that is a reserved word in Python.) Returned values range from 0 to positive infinity if lambd is positive, and from negative infinity to 0 if lambd is negative.
gammavariate(self, alpha, beta) Gamma distribution. Not the gamma function! Conditions on the parameters are alpha > 0 and beta > 0. The probability distribution function is: x ** (alpha - 1) * math.exp(-x / beta) pdf(x) = -------------------------------------- math.gamma(alpha) * beta ** alpha
gauss(self, mu, sigma) Gaussian distribution. mu is the mean, and sigma is the standard deviation. This is slightly faster than the normalvariate() function. Not thread-safe without a lock around calls.
getrandbits(self, k, /) getrandbits(k) -> x. Generates an int with k random bits.
getstate(self) Return internal state; can be passed to setstate() later.
lognormvariate(self, mu, sigma) Log normal distribution. If you take the natural logarithm of this distribution, you'll get a normal distribution with mean mu and standard deviation sigma. mu can have any value, and sigma must be greater than zero.
normalvariate(self, mu, sigma) Normal distribution. mu is the mean, and sigma is the standard deviation.
paretovariate(self, alpha) Pareto distribution. alpha is the shape parameter.
randbytes(self, n) Generate n random bytes.
randint(self, a, b) Return random integer in range [a, b], including both end points.
random(self, /) random() -> x in the interval [0, 1).
randrange(self, start, stop=None, step=1) Choose a random item from range(start, stop[, step]). This fixes the problem with randint() which includes the endpoint; in Python this is usually not what you want.
sample(self, population, k, *, counts=None) Chooses k unique random elements from a population sequence or set. Returns a new list containing elements from the population while leaving the original population unchanged. The resulting list is in selection order so that all sub-slices will also be valid random samples. This allows raffle winners (the sample) to be partitioned into grand prize and second place winners (the subslices). Members of the population need not be hashable or unique. If the population contains repeats, then each occurrence is a possible selection in the sample. Repeated elements can be specified one at a time or with the optional counts parameter. For example: sample(['red', 'blue'], counts=[4, 2], k=5) is equivalent to: sample(['red', 'red', 'red', 'red', 'blue', 'blue'], k=5) To choose a sample from a range of integers, use range() for the population argument. This is especially fast and space efficient for sampling from a large population: sample(range(10000000), 60)
seed(self, a=None, version=2) Initialize internal state from a seed. The only supported seed types are None, int, float, str, bytes, and bytearray. None or no argument seeds from current time or from an operating system specific randomness source if available. If *a* is an int, all bits are used. For version 2 (the default), all of the bits are used if *a* is a str, bytes, or bytearray. For version 1 (provided for reproducing random sequences from older versions of Python), the algorithm for str and bytes generates a narrower range of seeds.
setstate(self, state) Restore internal state from object returned by getstate().
shuffle(self, x, random=None) Shuffle list x in place, and return None. Optional argument random is a 0-argument function returning a random float in [0.0, 1.0); if it is the default None, the standard random.random will be used.
triangular(self, low=0.0, high=1.0, mode=None) Triangular distribution. Continuous distribution bounded by given lower and upper limits, and having a given mode value in-between. http://en.wikipedia.org/wiki/Triangular_distribution
uniform(self, a, b) Get a random number in the range [a, b) or [a, b] depending on rounding.
vonmisesvariate(self, mu, kappa) Circular data distribution. mu is the mean angle, expressed in radians between 0 and 2*pi, and kappa is the concentration parameter, which must be greater than or equal to zero. If kappa is equal to zero, this distribution reduces to a uniform random angle over the range 0 to 2*pi.
weibullvariate(self, alpha, beta) Weibull distribution. alpha is the scale parameter and beta is the shape parameter.
VERSION = 3
Alternate random number generator using sources provided by the operating system (such as /dev/urandom on Unix or CryptGenRandom on Windows). Not available on all systems (see os.urandom() for details).
betavariate(self, alpha, beta) Beta distribution. Conditions on the parameters are alpha > 0 and beta > 0. Returned values range between 0 and 1.
choice(self, seq) Choose a random element from a non-empty sequence.
choices(self, population, weights=None, *, cum_weights=None, k=1) Return a k sized list of population elements chosen with replacement. If the relative weights or cumulative weights are not specified, the selections are made with equal probability.
expovariate(self, lambd) Exponential distribution. lambd is 1.0 divided by the desired mean. It should be nonzero. (The parameter would be called "lambda", but that is a reserved word in Python.) Returned values range from 0 to positive infinity if lambd is positive, and from negative infinity to 0 if lambd is negative.
gammavariate(self, alpha, beta) Gamma distribution. Not the gamma function! Conditions on the parameters are alpha > 0 and beta > 0. The probability distribution function is: x ** (alpha - 1) * math.exp(-x / beta) pdf(x) = -------------------------------------- math.gamma(alpha) * beta ** alpha
gauss(self, mu, sigma) Gaussian distribution. mu is the mean, and sigma is the standard deviation. This is slightly faster than the normalvariate() function. Not thread-safe without a lock around calls.
getrandbits(self, k) getrandbits(k) -> x. Generates an int with k random bits.
_notimplemented(self, *args, **kwds) Method should not be called for a system random number generator.
lognormvariate(self, mu, sigma) Log normal distribution. If you take the natural logarithm of this distribution, you'll get a normal distribution with mean mu and standard deviation sigma. mu can have any value, and sigma must be greater than zero.
normalvariate(self, mu, sigma) Normal distribution. mu is the mean, and sigma is the standard deviation.
paretovariate(self, alpha) Pareto distribution. alpha is the shape parameter.
randbytes(self, n) Generate n random bytes.
randint(self, a, b) Return random integer in range [a, b], including both end points.
random(self) Get the next random number in the range [0.0, 1.0).
randrange(self, start, stop=None, step=1) Choose a random item from range(start, stop[, step]). This fixes the problem with randint() which includes the endpoint; in Python this is usually not what you want.
sample(self, population, k, *, counts=None) Chooses k unique random elements from a population sequence or set. Returns a new list containing elements from the population while leaving the original population unchanged. The resulting list is in selection order so that all sub-slices will also be valid random samples. This allows raffle winners (the sample) to be partitioned into grand prize and second place winners (the subslices). Members of the population need not be hashable or unique. If the population contains repeats, then each occurrence is a possible selection in the sample. Repeated elements can be specified one at a time or with the optional counts parameter. For example: sample(['red', 'blue'], counts=[4, 2], k=5) is equivalent to: sample(['red', 'red', 'red', 'red', 'blue', 'blue'], k=5) To choose a sample from a range of integers, use range() for the population argument. This is especially fast and space efficient for sampling from a large population: sample(range(10000000), 60)
seed(self, *args, **kwds) Stub method. Not used for a system random number generator.
_notimplemented(self, *args, **kwds) Method should not be called for a system random number generator.
shuffle(self, x, random=None) Shuffle list x in place, and return None. Optional argument random is a 0-argument function returning a random float in [0.0, 1.0); if it is the default None, the standard random.random will be used.
triangular(self, low=0.0, high=1.0, mode=None) Triangular distribution. Continuous distribution bounded by given lower and upper limits, and having a given mode value in-between. http://en.wikipedia.org/wiki/Triangular_distribution
uniform(self, a, b) Get a random number in the range [a, b) or [a, b] depending on rounding.
vonmisesvariate(self, mu, kappa) Circular data distribution. mu is the mean angle, expressed in radians between 0 and 2*pi, and kappa is the concentration parameter, which must be greater than or equal to zero. If kappa is equal to zero, this distribution reduces to a uniform random angle over the range 0 to 2*pi.
weibullvariate(self, alpha, beta) Weibull distribution. alpha is the scale parameter and beta is the shape parameter.
VERSION = 3
betavariate(alpha, beta) Beta distribution. Conditions on the parameters are alpha > 0 and beta > 0. Returned values range between 0 and 1.
choice(seq) Choose a random element from a non-empty sequence.
choices(population, weights=None, *, cum_weights=None, k=1) Return a k sized list of population elements chosen with replacement. If the relative weights or cumulative weights are not specified, the selections are made with equal probability.
expovariate(lambd) Exponential distribution. lambd is 1.0 divided by the desired mean. It should be nonzero. (The parameter would be called "lambda", but that is a reserved word in Python.) Returned values range from 0 to positive infinity if lambd is positive, and from negative infinity to 0 if lambd is negative.
gammavariate(alpha, beta) Gamma distribution. Not the gamma function! Conditions on the parameters are alpha > 0 and beta > 0. The probability distribution function is: x ** (alpha - 1) * math.exp(-x / beta) pdf(x) = -------------------------------------- math.gamma(alpha) * beta ** alpha
gauss(mu, sigma) Gaussian distribution. mu is the mean, and sigma is the standard deviation. This is slightly faster than the normalvariate() function. Not thread-safe without a lock around calls.
getrandbits(k, /) getrandbits(k) -> x. Generates an int with k random bits.
getstate() Return internal state; can be passed to setstate() later.
lognormvariate(mu, sigma) Log normal distribution. If you take the natural logarithm of this distribution, you'll get a normal distribution with mean mu and standard deviation sigma. mu can have any value, and sigma must be greater than zero.
normalvariate(mu, sigma) Normal distribution. mu is the mean, and sigma is the standard deviation.
paretovariate(alpha) Pareto distribution. alpha is the shape parameter.
randbytes(n) Generate n random bytes.
randint(a, b) Return random integer in range [a, b], including both end points.
random() random() -> x in the interval [0, 1).
randrange(start, stop=None, step=1) Choose a random item from range(start, stop[, step]). This fixes the problem with randint() which includes the endpoint; in Python this is usually not what you want.
sample(population, k, *, counts=None) Chooses k unique random elements from a population sequence or set. Returns a new list containing elements from the population while leaving the original population unchanged. The resulting list is in selection order so that all sub-slices will also be valid random samples. This allows raffle winners (the sample) to be partitioned into grand prize and second place winners (the subslices). Members of the population need not be hashable or unique. If the population contains repeats, then each occurrence is a possible selection in the sample. Repeated elements can be specified one at a time or with the optional counts parameter. For example: sample(['red', 'blue'], counts=[4, 2], k=5) is equivalent to: sample(['red', 'red', 'red', 'red', 'blue', 'blue'], k=5) To choose a sample from a range of integers, use range() for the population argument. This is especially fast and space efficient for sampling from a large population: sample(range(10000000), 60)
seed(a=None, version=2) Initialize internal state from a seed. The only supported seed types are None, int, float, str, bytes, and bytearray. None or no argument seeds from current time or from an operating system specific randomness source if available. If *a* is an int, all bits are used. For version 2 (the default), all of the bits are used if *a* is a str, bytes, or bytearray. For version 1 (provided for reproducing random sequences from older versions of Python), the algorithm for str and bytes generates a narrower range of seeds.
setstate(state) Restore internal state from object returned by getstate().
shuffle(x, random=None) Shuffle list x in place, and return None. Optional argument random is a 0-argument function returning a random float in [0.0, 1.0); if it is the default None, the standard random.random will be used.
triangular(low=0.0, high=1.0, mode=None) Triangular distribution. Continuous distribution bounded by given lower and upper limits, and having a given mode value in-between. http://en.wikipedia.org/wiki/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. mu is the mean angle, expressed in radians between 0 and 2*pi, and kappa is the concentration parameter, which must be greater than or equal to zero. If kappa is equal to zero, this distribution reduces to a uniform random angle over the range 0 to 2*pi.
weibullvariate(alpha, beta) Weibull distribution. alpha is the scale parameter and beta is the shape parameter.
BPF = 53
LOG4 = 1.3862943611198906
NV_MAGICCONST = 1.7155277699214135
RECIP_BPF = 1.1102230246251565e-16
SG_MAGICCONST = 2.504077396776274
TWOPI = 6.283185307179586