all Python 3randomfunction
random.betavariate
distributions
A random number from the beta distribution (alpha>0, beta>0); always in [0, 1].
Syntax
random.betavariate(alpha, beta)
Beta is the go-to when you need a random probability rather than a random number: the mean is alpha/(alpha+beta), and the larger the two parameters are, the tighter the values cluster around that mean. With alpha = beta = 1 it degenerates into the plain uniform distribution on [0, 1]; with both parameters below 1 the values pile up at the ends instead. Zero or negative parameters raise ValueError.
Examples
import random
print(0.0 <= random.betavariate(2.0, 3.0) <= 1.0) # → True
print(type(random.betavariate(2.0, 3.0)).__name__) # → float
print(round(sum(random.betavariate(2.0, 2.0) for _ in range(10000)) / 10000, 1)) # → 0.5
print(sum(random.betavariate(8.0, 2.0) for _ in range(10000)) / 10000 > 0.5) # → True
print(random.betavariate(0.0, 3.0)) # → ValueError
See also
Common errors
Open in the full glossarydocs.python.orgReport an inaccuracy