You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
random module binomialvariate function returns wrong results #149221
"""PoC: binomialvariate crash caused by random() returning 0.0.Run: python bug_demo.py"""frommathimportlog2, floordefsimulate_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=0iter_count=0whileTrue:
iter_count+=1rand_val=0.0# simulate the rare event of hitting exactly 0.0try:
log_rand=log2(rand_val)
exceptValueErrorase:
print(f"Crashed at iteration {iter_count}: {e}")
returnNone, iter_county+=floor(log_rand/c) +1ify>n:
returnx, iter_countx+=1if__name__=="__main__":
c=log2(0.5) # -1.0result, count=simulate_crash(c, n=1)
ifresultisNone:
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.
changed the title [-]random mod `binomialvariate` function returns wrong results[/-][+]random module `binomialvariate` function returns wrong results[/+]on May 1, 2026
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.
Activity
[-]random mod `binomialvariate` function returns wrong results[/-][+]random module `binomialvariate` function returns wrong results[/+]rhettinger commentedon May 2, 2026
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.
lighting9999 commentedon May 2, 2026
I'm sorry . I'm wirte error. I solve it.
gh-149221:Fix binomialvariate Function for random module (gh-149222)
[3.14]gh-149221:Fix binomialvariate Function for random module (gh-14…
gh-149221: Minor comment edit (gh-149278)
[3.13]pythongh-149221:Fix binomialvariate Function for random module
[3.14] gh-149221: Sync random.py with main branch (#149288)
[3.14] gh-149221: Add missing `test_binomialvariate_log_zero` (#154879)
[3.13] gh-149221:Fix binomialvariate Function for random module (#149279