Real-Valued Distributions: Random

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 5

Real-valued distributions

The following functions generate specific real-valued distributions. Function parameters


are named after the corresponding variables in the distribution’s equation, as used in
common mathematical practice; most of these equations can be found in any statistics
text.

random.random()

Return the next random floating point number in the range [0.0, 1.0).

random.uniform(a, b)

Return a random floating point number N such


that a <= N <= b for a <= b and b <= N <= a for b < a.

The end-point value b may or may not be included in the range depending on


floating-point rounding in the equation a + (b-a) * random().

random.triangular(low, high, mode)

Return a random floating point number N such that low <= N <= high and with


the specified mode between those bounds. The low and high bounds default to
zero and one. The mode argument defaults to the midpoint between the bounds,
giving a symmetric distribution.

random.betavariate(alpha, beta)

Beta distribution. Conditions on the parameters are alpha > 0 and beta > 0.


Returned values range between 0 and 1.

random.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.

random.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
random.gauss(mu, sigma)

Gaussian distribution. mu is the mean, and sigma is the standard deviation. This


is slightly faster than the normalvariate() function defined below.

random.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.

random.normalvariate(mu, sigma)

Normal distribution. mu is the mean, and sigma is the standard deviation.

random.vonmisesvariate(mu, kappa)

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.

random.paretovariate(alpha)

Pareto distribution. alpha is the shape parameter.

random.weibullvariate(alpha, 
beta)

Weibull distribution. alpha is the scale parameter and beta is the shape


parameter.

Alternative Generator
class  random.Random([seed])

Class that implements the default pseudo-random number generator used by


the random module.
Deprecated since version 3.9: In the future, the seed must be one of the
following types: NoneType, int, float, str, bytes, or bytearray.

class  random.SystemRando
m([seed])
Class that uses the os.urandom() function for generating random numbers
from sources provided by the operating system. Not available on all systems.
Does not rely on software state, and sequences are not reproducible.
Accordingly, the seed() method has no effect and is ignored.
The getstate() and setstate() methods raise NotImplementedError if
called.

Notes on
Reproducibility
Sometimes it is useful to be
able to reproduce the
sequences given by a
pseudo-random number
generator. By re-using a
seed value, the same
sequence should be
reproducible from run to run
as long as multiple threads
are not running.

Most of the random


module’s algorithms and
seeding functions are
subject to change across
Python versions, but two
aspects are guaranteed not
to change:

 If a new seeding
method is added,
then a backward
compatible seeder
will be offered.
 The
generator’s random(
) method will
continue to produce
the same sequence
when the compatible
seeder is given the
same seed.
Examples
Basic examples:

>>>

>>> random()
# Random float: 0.0
<= x < 1.0
0.37444887175646646

>>> uniform(2.5, 10.0)


# Random float: 2.5
<= x < 10.0
3.1800146073117523

>>> expovariate(1 / 5)
# Interval between
arrivals averaging 5
seconds
5.148957571865031

>>> randrange(10)
# Integer from 0 to 9
inclusive
7

>>> randrange(0, 101,


2) #
Even integer from 0 to
100 inclusive
26
>>> choice(['win',
'lose', 'draw'])
# Single random
element from a
sequence
'draw'

>>> deck = 'ace two


three four'.split()
>>> shuffle(deck)
# Shuffle a list
>>> deck
['four', 'two', 'ace',
'three']

>>> sample([10, 20,


30, 40, 50], k=4) #
Four samples without
replacement
[40, 10, 50, 30]

Simulations:

You might also like