diff --git a/Lib/random.py b/Lib/random.py index 365a01957203f2..c6d487db85ac6e 100644 --- a/Lib/random.py +++ b/Lib/random.py @@ -704,6 +704,17 @@ def weibullvariate(self, alpha, beta): u = 1.0 - self.random() return alpha * (-_log(u)) ** (1.0/beta) +## -------------------- Binomial distribution -------------------- + + def binomialvariate(n, p): + """ Binomial distribution for the number of successes + in *n* Bernoulli trials each with a probability *p* + of success. + + Returns an integer in the range: 0 <= X <= n + """ + return sum(self.random() < p for i in range(n)) + ## --------------- Operating System Random Source ------------------ class SystemRandom(Random):