Python Glossary Русский

all Python 3randomfunction

random.lognormvariate

distributions

A random number from the log-normal distribution (its logarithm is normal with mu, sigma); the result is always positive.

Syntax

random.lognormvariate(mu, sigma)

mu and sigma describe the logarithm of the result, not the result itself: the median is exp(mu) and the mean is exp(mu + sigma^2/2). Feeding in the mean and spread you want for the actual numbers is the usual mistake. The shape is right-skewed — a long tail of large values pulls the mean well above the median — which is exactly why it fits incomes, response times and file sizes.

Examples

import random
print(type(random.lognormvariate(0.0, 1.0)).__name__)   # → float (значение случайное, медиана ≈ 1.0)
print(random.lognormvariate(0.0, 1.0) > 0.0)   # → True
print(random.lognormvariate(0.0, 0.0))   # → 1.0
sample = [random.lognormvariate(0.0, 1.0) for _ in range(1000)]
print(all(x > 0.0 for x in sample))   # → True

See also

Open in the full glossarydocs.python.orgReport an inaccuracy