From 9392058d88515da8a589f13f007e394e7ad66c9a Mon Sep 17 00:00:00 2001 From: lkk7 Date: Sat, 18 Jul 2026 17:11:57 +0200 Subject: [PATCH 1/4] gh-154001: Avoid division by zero in `binomialvariate` --- Lib/random.py | 2 ++ Lib/test/test_random.py | 8 ++++++++ .../2026-07-18-17-09-49.gh-issue-154001.3brrUv.rst | 2 ++ 3 files changed, 12 insertions(+) create mode 100644 Misc/NEWS.d/next/Library/2026-07-18-17-09-49.gh-issue-154001.3brrUv.rst diff --git a/Lib/random.py b/Lib/random.py index 4541267bab866a4..de6588002571bcf 100644 --- a/Lib/random.py +++ b/Lib/random.py @@ -861,6 +861,8 @@ def binomialvariate(self, n=1, p=0.5): u = random() u -= 0.5 us = 0.5 - _fabs(u) + if us == 0.0: + continue k = _floor((2.0 * a / us + b) * u + c) if k < 0 or k > n: continue diff --git a/Lib/test/test_random.py b/Lib/test/test_random.py index dbd3b855f536a0d..8d093ab1b7014a4 100644 --- a/Lib/test/test_random.py +++ b/Lib/test/test_random.py @@ -1082,6 +1082,14 @@ def test_binomialvariate_log_zero(self): self.assertIsInstance(result, int) self.assertIn(result, range(11)) + def test_binomialvariate_btrs_random_zero(self): + for p, expected in ((0.25, 25), (0.75, 75)): + with self.subTest(p=p): + g = random.Random() + with unittest.mock.patch.object( + g, 'random', side_effect=(0.0, 0.5, 0.5)): + self.assertEqual(g.binomialvariate(100, p), expected) + def test_constant(self): g = random.Random() N = 100 diff --git a/Misc/NEWS.d/next/Library/2026-07-18-17-09-49.gh-issue-154001.3brrUv.rst b/Misc/NEWS.d/next/Library/2026-07-18-17-09-49.gh-issue-154001.3brrUv.rst new file mode 100644 index 000000000000000..cfcf31d23273676 --- /dev/null +++ b/Misc/NEWS.d/next/Library/2026-07-18-17-09-49.gh-issue-154001.3brrUv.rst @@ -0,0 +1,2 @@ +Fix :meth:`random.Random.binomialvariate` raising :exc:`ZeroDivisionError` +when a random draw is zero. From cddf77cf99335fe0421132e8f414174dbb5cb92f Mon Sep 17 00:00:00 2001 From: lkk7 Date: Sat, 18 Jul 2026 17:25:13 +0200 Subject: [PATCH 2/4] gh-154001: Fix NEWS reference --- .../next/Library/2026-07-18-17-09-49.gh-issue-154001.3brrUv.rst | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Misc/NEWS.d/next/Library/2026-07-18-17-09-49.gh-issue-154001.3brrUv.rst b/Misc/NEWS.d/next/Library/2026-07-18-17-09-49.gh-issue-154001.3brrUv.rst index cfcf31d23273676..901f54d51b98912 100644 --- a/Misc/NEWS.d/next/Library/2026-07-18-17-09-49.gh-issue-154001.3brrUv.rst +++ b/Misc/NEWS.d/next/Library/2026-07-18-17-09-49.gh-issue-154001.3brrUv.rst @@ -1,2 +1,2 @@ -Fix :meth:`random.Random.binomialvariate` raising :exc:`ZeroDivisionError` +Fix :func:`random.binomialvariate` raising :exc:`ZeroDivisionError` when a random draw is zero. From e2560d617e4ee2600c14c26c2d900d5ead1a3658 Mon Sep 17 00:00:00 2001 From: lkk7 Date: Sat, 18 Jul 2026 17:30:04 +0200 Subject: [PATCH 3/4] gh-154001: Drop unnecessary NEWS attributes --- .../next/Library/2026-07-18-17-09-49.gh-issue-154001.3brrUv.rst | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Misc/NEWS.d/next/Library/2026-07-18-17-09-49.gh-issue-154001.3brrUv.rst b/Misc/NEWS.d/next/Library/2026-07-18-17-09-49.gh-issue-154001.3brrUv.rst index 901f54d51b98912..3690061e5ceb712 100644 --- a/Misc/NEWS.d/next/Library/2026-07-18-17-09-49.gh-issue-154001.3brrUv.rst +++ b/Misc/NEWS.d/next/Library/2026-07-18-17-09-49.gh-issue-154001.3brrUv.rst @@ -1,2 +1,2 @@ -Fix :func:`random.binomialvariate` raising :exc:`ZeroDivisionError` +Fix ``random.Random.binomialvariate()`` raising :exc:`ZeroDivisionError` when a random draw is zero. From e108916cb8f7ce40cf3ac808f661dab12d18e24f Mon Sep 17 00:00:00 2001 From: lkk7 Date: Sat, 18 Jul 2026 22:17:55 +0200 Subject: [PATCH 4/4] gh-154001: Address review comments --- Lib/random.py | 6 ++++-- .../Library/2026-07-18-17-09-49.gh-issue-154001.3brrUv.rst | 4 ++-- 2 files changed, 6 insertions(+), 4 deletions(-) diff --git a/Lib/random.py b/Lib/random.py index de6588002571bcf..7db761034509d37 100644 --- a/Lib/random.py +++ b/Lib/random.py @@ -861,9 +861,11 @@ def binomialvariate(self, n=1, p=0.5): u = random() u -= 0.5 us = 0.5 - _fabs(u) - if us == 0.0: + try: + k = _floor((2.0 * a / us + b) * u + c) + except ZeroDivisionError: + # Reject case where random() returned 0.0 continue - k = _floor((2.0 * a / us + b) * u + c) if k < 0 or k > n: continue v = random() diff --git a/Misc/NEWS.d/next/Library/2026-07-18-17-09-49.gh-issue-154001.3brrUv.rst b/Misc/NEWS.d/next/Library/2026-07-18-17-09-49.gh-issue-154001.3brrUv.rst index 3690061e5ceb712..ff019aa36188479 100644 --- a/Misc/NEWS.d/next/Library/2026-07-18-17-09-49.gh-issue-154001.3brrUv.rst +++ b/Misc/NEWS.d/next/Library/2026-07-18-17-09-49.gh-issue-154001.3brrUv.rst @@ -1,2 +1,2 @@ -Fix ``random.Random.binomialvariate()`` raising :exc:`ZeroDivisionError` -when a random draw is zero. +Fix :func:`random.binomialvariate` raising :exc:`ZeroDivisionError` +when :func:`random.random` returns zero.