Skip to content

Commit a6d7bf5

Browse files
authored
[3.14]gh-149221:Fix binomialvariate Function for random module (gh-149276)
1 parent 4f8399f commit a6d7bf5

2 files changed

Lines changed: 7 additions & 1 deletion

File tree

Lib/random.py

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -836,7 +836,12 @@ def binomialvariate(self, n=1, p=0.5):
836836
if not c:
837837
return x
838838
while True:
839-
y += _floor(_log2(random()) / c) + 1
839+
try:
840+
y += _floor(_log2(random()) / c) + 1
841+
# The random() function can return 0.0, which causes log2(0.0) to raise a ValueError.
842+
# See https://github.com/python/cpython/issue/149221
843+
except ValueError:
844+
continue
840845
if y > n:
841846
return x
842847
x += 1
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Catch rare math domain error for :func:`random.binomialvariate`.

0 commit comments

Comments
 (0)