random.gammavariate / betavariate

gammavariate is the gamma DISTRIBUTION, not math.gamma. Its beta is a scale (mean = alpha * beta); some textbooks use a rate instead. betavariate(alpha, beta) draws two gammas and returns y / (y + z).

random functionAll Python 3 versionsLive demo
Common call
random.betavariate(2, 5)
Returns
float (gamma: > 0, beta: in [0, 1])
Replaces
scipy.stats / numpy for single draws
Watch out
gamma beta is a scale: rate r means beta = 1 / r
random.gammavariate(alphaalpha — Shape, > 0. For betavariate: the "successes" side.type: float · required, beta) / random.betavariate(alpha, betabeta — gammavariate: scale (> 0, mean = alpha * beta). betavariate: second shape (> 0).type: float · required)
→ float

Demo

Live evaluation
Five gamma draws, rounded. alpha = 1 is the exponential distribution with mean beta.
Try:
Inputs
seedint | stran int or a string
alphafloatshape
betafloatscale
Code
import random
random.seed(42)
[round(random.gammavariate(2.0, 1.0), 4) for _ in range(5)]
Result
[1.1429, 3.6201, 1.6675, 0.9587, 0.2499]

gammavariate switches algorithm on alpha: above 1 it uses Cheng's rejection method, exactly 1 is -log(1 - random()) * beta (so alpha 1, beta 3 gives three times the expovariate(1.0) values), below 1 it uses algorithm GS with x ** (1 / alpha). betavariate(2, 5) has mean 2 / 7, so most draws are small. The values are rounded because log, exp and ** come from the platform C library.

Parameters

NameTypeRequiredDescription
alphafloatyesShape, > 0. For betavariate: the "successes" side.
betafloatyesgammavariate: scale (> 0, mean = alpha * beta). betavariate: second shape (> 0).

Return value

float — gammavariate: x > 0 with mean alpha * beta. betavariate: 0 <= x <= 1 with mean alpha / (alpha + beta).

Common patterns

Bayesian estimate of a rate
Thompson sampling: draw a plausible conversion rate per variant, pick the best.
import random
samples = {v: random.betavariate(wins[v] + 1, losses[v] + 1) for v in variants}
choice = max(samples, key=samples.get)
Gamma with a rate parameter
Convert rate to scale.
import random
x = random.gammavariate(shape, 1 / rate)
Random proportions that sum to 1
Normalize independent gamma draws (a Dirichlet sample).
import random
g = [random.gammavariate(a, 1.0) for a in (2.0, 3.0, 5.0)]
shares = [x / sum(g) for x in g]

Examples

1. gammavariate(2.0, 1.0)
import random random.seed(42) random.gammavariate(2.0, 1.0)
Returns
1.1428765860350905
2. alpha < 1 uses another algorithm
import random random.seed(42) random.gammavariate(0.5, 1.0)
Returns
0.573113778473856
3. alpha == 1: exponential with mean beta
import random random.seed(42) random.gammavariate(1.0, 2.0)
Returns
2.040120574549602
4. Mean is alpha * beta
import random random.seed(42) round(sum(random.gammavariate(2.0, 3.0) for _ in range(10000)) / 10000, 1)
Returns
5.9
5. betavariate(2, 5)
import random random.seed(42) random.betavariate(2, 5)
Returns
0.13961892090489622
6. Beta draws stay inside (0, 1)
import random random.seed(42) x = [random.betavariate(2, 5) for _ in range(1000)] (min(x) > 0, max(x) < 1)
Returns
(True, True)
7. Parameters must be positive
import random random.gammavariate(0, 1)
Returns
ValueError: gammavariate: alpha and beta must be > 0.0

Pitfalls

1. Rate instead of scale
Python's beta is the scale (mean alpha * beta). If your formula has a rate of 3, pass 1 / 3: passing 3 makes the mean nine times larger.
beta = rate
import random
random.seed(42)
round(sum(random.gammavariate(2.0, 3.0) for _ in range(10000)) / 10000, 2)
5.94
beta = 1 / rate
import random
random.seed(42)
round(sum(random.gammavariate(2.0, 1 / 3) for _ in range(10000)) / 10000, 2)
0.66
2. An error message that names the wrong function
betavariate calls gammavariate internally, so invalid beta parameters report "gammavariate".
betavariate(0, 1)
import random
random.seed(42)
random.betavariate(0, 1)
ValueError: gammavariate: alpha and beta must be > 0.0
positive shapes
import random
random.seed(42)
random.betavariate(2, 5) < 1
True

When to use

Use it
  • Waiting time until the alpha-th event, positive skewed quantities: gammavariate
  • Random probabilities and proportions, Bayesian priors: betavariate
Reach for something else
  • The gamma FUNCTION → math.gamma / math.lgamma
  • Exponential waiting times → expovariate
  • Many draws for statistics work → numpy.random.Generator.gamma / beta

Notes

CPython impl
Lib/random.py: alpha > 1 uses R.C.H. Cheng (1977) with log and exp; alpha == 1 is -log(1.0 - random()) * beta; 0 < alpha < 1 uses algorithm GS (Kennedy and Gentle) with ** and exp. betavariate: y = gammavariate(alpha, 1.0); returns y / (y + gammavariate(beta, 1.0)), or 0.0 when y is 0
Platforms
log, exp and ** come from the C library, so the last digit can differ between Windows, Linux and macOS for a small fraction of draws; the full-precision examples here were verified on Windows and Linux CPython
Check
Only gammavariate validates: alpha <= 0.0 or beta <= 0.0 raises ValueError

FAQ

No. It draws a random number from the gamma distribution. The gamma function itself is math.gamma(x) (and math.lgamma for its logarithm).