all Python 3randomfunction
random.gammavariate
distributions
A random number from the gamma distribution (alpha>0, beta>0); always positive.
Syntax
random.gammavariate(alpha, beta)
Here beta is the scale, so the mean is alpha*beta — that is why alpha=2, beta=3 averages around 6. Many textbooks and libraries define beta the other way round, as a rate, so a formula copied from elsewhere often has to be converted to 1/beta. This has nothing to do with the mathematical gamma function (math.gamma), and both alpha and beta must be strictly positive or you get a ValueError.
Examples
import random
print(random.gammavariate(2.0, 3.0) > 0.0) # → True
print(type(random.gammavariate(2.0, 3.0)).__name__) # → float
print(round(sum(random.gammavariate(2.0, 3.0) for _ in range(10000)) / 10000)) # → 6
print(random.gammavariate(1.0, 2.0) > 0.0) # → True
print(random.gammavariate(0.0, 3.0)) # → ValueError
See also
Common errors
Open in the full glossarydocs.python.orgReport an inaccuracy