Python Glossary Русский

all Python 3randomterm

random.gauss

distributions

Returns a random number from the normal (Gaussian) distribution with the parameters mu (the mean) and sigma (the standard deviation).

Syntax

random.gauss(mu=0.0, sigma=1.0)

The speed comes from a cache: one call computes a pair of values and hands out the second one on the next call, so two threads can end up with the same number. In threaded code use normalvariate or give each thread its own random.Random instance. The output is unbounded — roughly 0.3% of draws fall outside mu ± 3*sigma — so for quantities that must stay positive, clipping at zero skews the distribution more than simply drawing again.

Examples

import random
random.seed(42)  # зерно задано — числа воспроизводимы
print(random.gauss(0, 1))    # → -0.14409032957792836 — стандартное нормальное
print(random.gauss(100, 15)) # → 97.40644599502721
results = [random.gauss(0, 1) for _ in range(1000)]
print(round(sum(results) / len(results), 2))  # → -0.02 — среднее близко к 0
print(round((sum(x**2 for x in results) / len(results)) ** 0.5, 2))  # → 1.02 — разброс близок к 1

See also

Open in the full glossarydocs.python.orgReport an inaccuracy