From d9b69a5ba3d58e9998bc395788400827e229c50f Mon Sep 17 00:00:00 2001 From: Raymond Hettinger Date: Fri, 1 May 2026 23:32:06 -0500 Subject: [PATCH] gh-149221: Handle corner case in binomialvariate --- Lib/random.py | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/Lib/random.py b/Lib/random.py index c89cbb755abac8..2460ac3d98601f 100644 --- a/Lib/random.py +++ b/Lib/random.py @@ -836,7 +836,11 @@ 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 + except ValueError: + # Handle rare case of where random() gives 0.0 + continue if y > n: return x x += 1