Python Glossary Русский

all Python 3randomfunction

random.triangular

distributions

A random number from the triangular distribution on [low, high] with mode; within the bounds.

Syntax

random.triangular(low=0.0, high=1.0, mode=None)

With mode omitted the peak sits exactly midway between low and high, giving a symmetric triangle — people often assume the default leans toward low and get a different shape than intended. Reach for it in three-point estimation, when all you have is a minimum, a most-likely value and a maximum: unlike uniform it gives weight to the peak. The degenerate case low == high does not raise, it returns low.

Examples

import random
print(0.0 <= random.triangular(0.0, 10.0, 5.0) <= 10.0)   # → True
print(0.0 <= random.triangular() <= 1.0)   # → True
print(random.triangular(5.0, 5.0, 5.0))   # → 5.0
print(all(1 <= random.triangular(1, 10, 2) <= 10 for _ in range(1000)))   # → True
print(round(sum(random.triangular(0, 9, 0) for _ in range(20000)) / 20000))   # → 3

See also

Open in the full glossarydocs.python.orgReport an inaccuracy