Skip to content

Commit 734d2ad

Browse files
authored
[3.13]gh-149221:Fix binomialvariate Function for random module
Handle ValueError from random() in log2 calculation.
1 parent b274204 commit 734d2ad

1 file changed

Lines changed: 6 additions & 1 deletion

File tree

Lib/random.py

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -828,7 +828,12 @@ def binomialvariate(self, n=1, p=0.5):
828828
if not c:
829829
return x
830830
while True:
831-
y += _floor(_log2(random()) / c) + 1
831+
try:
832+
y += _floor(_log2(random()) / c) + 1
833+
# The random() function can return 0.0, which causes log2(0.0) to raise a ValueError.
834+
# See https://github.com/python/cpython/issue/149221
835+
except ValueError:
836+
continue
832837
if y > n:
833838
return x
834839
x += 1

0 commit comments

Comments
 (0)