Python Glossary Русский

all Python 3randomfunction

random.expovariate

distributions

A random number from the exponential distribution with rate lambd; always non-negative.

Syntax

random.expovariate(lambd=1.0)

lambd is a rate, not a mean: it equals one divided by the average you want, so an average gap of 5 needs 0.2, not 5. Passing the mean by mistake is the classic bug in queue and inter-arrival simulations. Zero is not allowed, a negative lambd mirrors the results into negative values, and calling it with no argument at all only works from Python 3.12 on.

Examples

import random
print(random.expovariate(1.0) >= 0.0)   # → True
print(round(sum(random.expovariate(2.0) for _ in range(10000)) / 10000, 1))   # → 0.5
print(random.expovariate(1000.0) < 1.0)   # → True
print(random.expovariate(0.0))   # → ZeroDivisionError
# Python 3.12+
import random
print(type(random.expovariate()).__name__)   # → float

See also

Common errors

Open in the full glossarydocs.python.orgReport an inaccuracy