8000 GH-81620: Add random.binomialvariate() by rhettinger · Pull Request #94719 · python/cpython · GitHub
[go: up one dir, main page]

Skip to content

GH-81620: Add random.binomialvariate() #94719

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

Merged
merged 19 commits into from
Jul 13, 2022
Merged
Changes from 1 commit
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
Prev Previous commit
Next Next commit
Neated-up comments
  • Loading branch information
rhettinger committed Jul 10, 2022
commit 05653a8e9f8c6debf533c203898a33e2401db9d8
19 changes: 8 additions & 11 deletions Lib/random.py
Original file line number Diff line number Diff line change
Expand Up @@ -785,7 +785,7 @@ def binomialvariate(self, n=1, p=0.5):

# Step 0: Setup for step 1
spq = _sqrt(n * p * (1.0 - p)) # Standard deviation of the distribution
b = 1.15 + 2.53 * spq # Dominating density function parameters
b = 1.15 + 2.53 * spq
a = -0.0873 + 0.0248 * b + 0.01 * p
c = n * p + 0.5
vr = 0.92 - 4.2 / b
Expand All @@ -798,30 +798,27 @@ def binomialvariate(self, n=1, p=0.5):
u -= 0.5
us = 0.5 - _fabs(u)
k = _floor((2.0 * a / us + b) * u + c)

# Step 2: Skip over invalid k arising due to numeric issues.
if k < 0 or k > n:
continue

# This early-out "squeeze" test substantially reduces the
# number of acceptance condition evaluations. Checks to see
# whether *us* and *vr* lie in the large rectangle between
# Step 2: This early-out "squeeze" test substantially reduces
# the number of acceptance condition evaluations. Checks to
# see whether *us* and *vr* lie in the large rectangle between
# the u-axis and the curve.
if us >= 0.07 and v <= vr:
return k

if not step_three_setup:
# Step 3.0: Compute constants for step 3.1
# Step 3.0: Set up constants for step 3.1
alpha = (2.83 + 5.1 / b) * spq
lpq = _log(p / (1.0 - p)) # Log of p / q ratio
lpq = _log(p / (1.0 - p))
m = _floor((n + 1) * p) # Mode of the distribution
h = _logfact(m) + _logfact(n - m)
step_three_setup = True # Only needs to be done once

# Step 3.1: Acceptance-rejection test.
# N.B. The original paper errorneously omits the call to
# log(v) which is needed because we're comparing to the log
# of the rescaled binomial distribution.
# N.B. The original paper errorneously omits the call to log(v)
# when comparing to the log of the rescaled binomial distribution.
v *= alpha / (a / (us * us) + b)
if _log(v) <= h - _logfact(k) - _logfact(n - k) + (k - m) * lpq:
return k
Expand Down
0