Each is a few lines of Python on top of random(): an inverse transform (expovariate, paretovariate, weibullvariate, triangular) or a rejection loop (vonmisesvariate). Watch the parameter conventions: expovariate takes a RATE, triangular takes (low, high, mode).
random functionsAll Python 3 versions (expovariate default lambd=1.0 since 3.12)Live demo
expovariate(1.0) and expovariate(0.2) use the same random() values, so the second list is (up to rounding) the first times 5. A negative rate mirrors them below zero, and rate 0 divides by zero. Around mu = 0 the von Mises angles wrap: values near 2*pi (about 6.2832) are close to 0. weibullvariate with shape 1 is the exponential distribution. Results are rounded because log, exp, cos, acos and ** come from the platform C library and can differ in the last digit.
Parameters
Name
Type
Required
Description
lambd
float
no (1.0)
expovariate: the rate, 1 / desired mean; nonzero (a negative rate gives values <= 0). Default since 3.12.
low, high, mode
float
no (0.0, 1.0, None)
triangular: the bounds and the peak; mode=None means the midpoint.
mu, sigma
float
yes
lognormvariate: mean and standard deviation of the underlying normal (of log(X)); sigma > 0.
alpha
float
yes
paretovariate: shape. weibullvariate: scale.
beta
float
yes
weibullvariate: shape.
mu, kappa
float
yes
vonmisesvariate: mean angle in radians and concentration >= 0; kappa <= 1e-6 gives a uniform angle in [0, 2*pi).
Return value
float — One draw from the distribution.
Common patterns
Arrival times of a Poisson process
Exponential gaps with mean 5.6 (the queue simulation from the random docs).
Reliability and lifetimes: weibullvariate; wealth and popularity tails: paretovariate
Directions and times of day on a circle: vonmisesvariate
Reach for something else
Normal noise → gauss / normalvariate
Proportions between 0 and 1 → betavariate; positive skewed with a shape → gammavariate
Counts of successes → binomialvariate
Notes
CPython impl
Lib/random.py: expovariate = -log(1.0 - random()) / lambd; triangular inverts the CDF with one sqrt; lognormvariate = exp(normalvariate(mu, sigma)); paretovariate = (1.0 - random()) ** (-1.0 / alpha); weibullvariate = alpha * (-log(1.0 - random())) ** (1.0 / beta); vonmisesvariate is a rejection loop after N. I. Fisher (Statistical Analysis of Circular Data) using cos, exp and acos
Platforms
triangular uses only + - * / and sqrt, so it is exact everywhere. The others call log, exp, cos, acos or pow from the C library, whose last digit can differ between Windows, Linux and macOS for a small fraction of inputs; the full-precision examples on this page were verified on Windows and Linux CPython
Errors
ZeroDivisionError: float division by zero for expovariate(0), paretovariate(0) and weibullvariate(alpha, 0). OverflowError when a result exceeds the float range: math range error from exp (lognormvariate); from ** (paretovariate or weibullvariate with tiny parameters) the text is platform-dependent, (34, 'Result too large') on Windows and (34, 'Numerical result out of range') on Linux
The rate of the exponential distribution, 1 divided by the desired mean (the name avoids the keyword lambda). expovariate(0.5) has mean 2; since Python 3.12 the default is 1.0.