Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
23 commits
Select commit Hold shift + click to select a range
149c10c
Fix random number generation logic in random
lighting9999 May 1, 2026
af508ed
📜🤖 Added by blurb_it.
blurb-it[bot] May 1, 2026
6e420cb
Update Misc/NEWS.d/next/图书馆/2026-05-01-11-31-03.gh-issue-149221.lOw7D…
lighting9999 May 1, 2026
319bb0c
Fix logic and use try-expect
lighting9999 May 1, 2026
3f5b9ea
Change method
lighting9999 May 1, 2026
dc0a1af
Delete 2026-05-01-11-31-03.gh-issue-149221.lOw7Dy.rst
lighting9999 May 2, 2026
f298233
Fix logic and fix unexpected code block
lighting9999 May 2, 2026
c063df9
📜🤖 Added by blurb_it.
blurb-it[bot] May 2, 2026
278c11b
Add 'Test' for gh-149422
lighting9999 May 2, 2026
e7233dc
Change test
lighting9999 May 2, 2026
1ca8d09
Fix handling of log(0) case in random.py
lighting9999 May 2, 2026
a0fb874
Apply suggestion from @skirpichev
lighting9999 May 2, 2026
5649c59
Apply suggestion from @skirpichev
lighting9999 May 2, 2026
9d6eaf7
Clean up whitespace
lighting9999 May 2, 2026
95c7788
Refactor random number generation logic
lighting9999 May 2, 2026
cec01ec
Clean up whitespace
lighting9999 May 2, 2026
e73278a
Update random.py
lighting9999 May 2, 2026
d036e89
Fix exception handling in random and reuse try-except loop
lighting9999 May 2, 2026
dd833b9
Fix
lighting9999 May 2, 2026
e315b38
Fix error
lighting9999 May 2, 2026
206b82a
Apply suggestion from @ByteFlowing1337
lighting9999 May 2, 2026
32e5659
Apply suggestion from @ByteFlowing1337
lighting9999 May 2, 2026
922acf6
Merge branch 'main' into patch-1
lighting9999 May 2, 2026
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
7 changes: 6 additions & 1 deletion Lib/random.py
Original file line number Diff line number Diff line change
Expand Up @@ -836,7 +836,12 @@ def binomialvariate(self, n=1, p=0.5):
if not c:
return x
while True:
y += _floor(_log2(random()) / c) + 1
try:
y += _floor(_log2(random()) / c) + 1
# The random() function can return 0.0, which causes log2(0.0) to raise a ValueError.
# See https://github.com/python/cpython/issue/149221
except ValueError:
continue
if y > n:
Comment thread
lighting9999 marked this conversation as resolved.
return x
x += 1
Expand Down
6 changes: 6 additions & 0 deletions Lib/test/test_random.py
Original file line number Diff line number Diff line change
Expand Up @@ -1075,6 +1075,12 @@ def test_avg_std(self):
msg='%s%r' % (variate.__name__, args))
self.assertAlmostEqual(s2/(N-1), sigmasqrd, places=2,
msg='%s%r' % (variate.__name__, args))
def test_binomialvariate_log_zero(self):
# gh-149222: Variety random() return 0.0 no input Error
with unittest.mock.patch.object(random.Random, 'random', side_effect= [0.0] + [0.5] * 20):
result = random.binomialvariate(10, 0.5)
self.assertIsInstance(result, int)
self.assertIn(result, range(11))

def test_constant(self):
g = random.Random()
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Catch rare math domain error for :func:`random.binomialvariate`.
Loading