Skip to content

gh-149221: Fix binomialvariate() raising ValueError when random() returns 0.0#2

Open
JavaZeroo wants to merge 1 commit into
mainfrom
claude/quirky-johnson-n2JAj
Open

gh-149221: Fix binomialvariate() raising ValueError when random() returns 0.0#2
JavaZeroo wants to merge 1 commit into
mainfrom
claude/quirky-johnson-n2JAj

Conversation

@JavaZeroo

Copy link
Copy Markdown
Owner

Summary:

  • Fix random.binomialvariate() raising ValueError: math domain error when the underlying random() returns 0.0 (a value within its documented range [0.0, 1.0)). It occurs in the BG geometric branch (n * p < 10) where log2(random()) is evaluated.
  • Add a regression test for the random() == 0.0 case.

Tests:

  • ./python -m test test_random -m '*binomialvariate*'

Generated by Claude Code

…() 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

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💡 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".

Comment thread Lib/random.py
Comment on lines +839 to +843
try:
y += _floor(_log2(random()) / c) + 1
except ValueError:
# Reject case where random() returned 0.0
continue

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P2 Badge 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 👍 / 👎.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants