]> git.ipfire.org Git - thirdparty/Python/cpython.git/commitdiff
[3.14]gh-149221:Fix binomialvariate Function for random module (gh-149276)
authorlighting9999 <lightingcloud999@outlook.com>
Sat, 2 May 2026 12:57:42 +0000 (20:57 +0800)
committerGitHub <noreply@github.com>
Sat, 2 May 2026 12:57:42 +0000 (07:57 -0500)
Lib/random.py
Misc/NEWS.d/next/Library/2026-05-02-12-03-48.gh-issue-149221.__KOks.rst [new file with mode: 0644]

index 86d562f0b8aaf60eece0f1c1c8e240cd499b049e..69ab3a96f142dbd645ba36e591a63620dcbf5ab4 100644 (file)
@@ -836,7 +836,12 @@ class Random(_random.Random):
             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:
                     return x
                 x += 1
diff --git a/Misc/NEWS.d/next/Library/2026-05-02-12-03-48.gh-issue-149221.__KOks.rst b/Misc/NEWS.d/next/Library/2026-05-02-12-03-48.gh-issue-149221.__KOks.rst
new file mode 100644 (file)
index 0000000..fab2b0f
--- /dev/null
@@ -0,0 +1 @@
+Catch rare math domain error for :func:`random.binomialvariate`.