8000 bpo-37439 - Add random.binomialvariate() by avinassh · Pull Request #14530 · python/cpython · GitHub
[go: up one dir, main page]

Skip to content

bpo-37439 - Add random.binomialvariate() #14530

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
bpo-37439 - Add random.binomialvariate()
  • Loading branch information
avinassh authored Jul 1, 2019
commit 7a265b6d99e54c1570e9709e0befe0bc8e831721
11 changes: 11 additions & 0 deletions Lib/random.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down
0