You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
random.binomialvariate() can raise ZeroDivisionError when random() returns 0.0 #154001
random.binomialvariate() can raise ZeroDivisionError when random() returns 0.0 in one of the paths.
# Lib/random.pyu=random()
u-=0.5us=0.5-_fabs(u)
k=_floor((2.0*a/us+b) *u+c) # <-- division by `us` which can be zero
Repro with a mock that forces random() to return zero:
importrandomfromunittest.mockimportpatchwithpatch.object(random.Random, "random", side_effect=[0.0, 0.5, 0.5]):
random.binomialvariate(100, 0.25)
# k = _floor((2.0 * a / us + b) * u + c)# ~~~~~~~~^~~~# ZeroDivisionError: division by zero
From what I've seen, it looks like the chance of random returning zero is about 1 in 2^53... But still, zero is a valid result and custom generators may return it more often.
Activity
pythongh-154001: Avoid division by zero in `binomialvariate`
pythongh-154001: Fix NEWS reference
pythongh-154001: Drop unnecessary NEWS attributes
pythongh-154001: Address review comments
gh-154001: Avoid division by zero in binomialvariate (GH-154004)
serhiy-storchaka commentedon Jul 19, 2026
Thank you for your contribution @lkk7.
[3.13] gh-154001: Avoid division by zero in binomialvariate (GH-154004)…
[3.14] gh-154001: Avoid division by zero in binomialvariate (GH-154004)…
[3.15] gh-154001: Avoid division by zero in binomialvariate (GH-154004)…