gh-149221: Fix binomialvariate() raising ValueError when random() returns 0.0#2
gh-149221: Fix binomialvariate() raising ValueError when random() returns 0.0#2JavaZeroo wants to merge 1 commit into
Conversation
…() returns 0.0 random() may return 0.0 (its documented range is [0.0, 1.0)), which made the BG geometric branch of binomialvariate() evaluate log2(0.0) and raise "ValueError: math domain error". Reject and resample that draw. https://claude.ai/code/session_019aJ7pxZ6gNWzXmYT5obbvN
There was a problem hiding this comment.
💡 Codex Review
Here are some automated review suggestions for this pull request.
Reviewed commit: 6454612166
ℹ️ About Codex in GitHub
Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you
- Open a pull request for review
- Mark a draft as ready
- Comment "@codex review".
If Codex has suggestions, it will comment; otherwise it will react with 👍.
Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".
| try: | ||
| y += _floor(_log2(random()) / c) + 1 | ||
| except ValueError: | ||
| # Reject case where random() returned 0.0 | ||
| continue |
There was a problem hiding this comment.
Limit suppression to the zero draw
When a Random subclass's random() raises ValueError (for example, because its entropy source fails), this handler catches that exception as though the method returned zero and retries forever instead of propagating the failure. It also loops on invalid negative or NaN return values. Call random() outside the try and explicitly retry only when the returned value is 0.0, so unrelated errors retain their previous behavior.
Useful? React with 👍 / 👎.
Summary:
random.binomialvariate()raisingValueError: math domain errorwhen the underlyingrandom()returns0.0(a value within its documented range[0.0, 1.0)). It occurs in the BG geometric branch (n * p < 10) wherelog2(random())is evaluated.random() == 0.0case.Tests:
./python -m test test_random -m '*binomialvariate*'Generated by Claude Code