Skip to content

random module binomialvariate function returns wrong results #149221

Closed

Description

@lighting9999

Bug report

Bug description:

poc:

"""
PoC: binomialvariate crash caused by random() returning 0.0.
Run: python bug_demo.py
"""

from math import log2, floor

def simulate_crash(c, n):
    """Simulate the buggy BG branch with random() hardcoded to 0.0.
    Records the iteration count at which the crash occurs."""
    x = y = 0
    iter_count = 0
    while True:
        iter_count += 1
        rand_val = 0.0            # simulate the rare event of hitting exactly 0.0
        try:
            log_rand = log2(rand_val)
        except ValueError as e:
            print(f"Crashed at iteration {iter_count}: {e}")
            return None, iter_count

        y += floor(log_rand / c) + 1
        if y > n:
            return x, iter_count
        x += 1

if __name__ == "__main__":
    c = log2(0.5)   # -1.0
    result, count = simulate_crash(c, n=1)
    if result is None:
        print(f"Program crashed after {count} iteration(s) (crashed immediately).")
    else:
        print(f"Result {result}, loop ran {count} iteration(s)")

Actual behavior:

· When random() returns exactly 0.0 (which is within the documented range [0.0, 1.0)), _log2(0.0) raises a ValueError: math domain error, crashing the program.

Expected behavior:

· The function should never crash on a valid random value.
· The returned values should accurately follow the binomial distribution with parameters n and p.

CPython versions tested on:

CPython main branch

Operating systems tested on:

Other, Linux

Linked PRs

Activity

  1. changed the title [-]random mod `binomialvariate` function returns wrong results[/-] [+]random module `binomialvariate` function returns wrong results[/+] on May 1, 2026
  2. added
    stdlibStandard Library Python modules in the Lib/ directory
    on May 1, 2026
  3. self-assigned this
    on May 1, 2026
  4. rhettinger commented on May 2, 2026

    @rhettinger
    Contributor

    This looks like an LLM generated report.

    Can you explain this part, "Even when random() never returns 0.0, the generated distribution is wrong – it does not match the intended binomial distribution." That doesn't make any sense.

  5. lighting9999 commented on May 2, 2026

    @lighting9999
    ContributorAuthor

    I'm sorry . I'm wirte error. I solve it.

  6. added a commit that references this issue on May 2, 2026
  7. added 2 commits that reference this issue on May 2, 2026
  8. added a commit that references this issue on May 2, 2026
  9. added a commit that references this issue on May 2, 2026
  10. added 2 commits that reference this issue on Jul 29, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

stdlibStandard Library Python modules in the Lib/ directorytype-bugAn unexpected behavior, bug, or error

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions