Skip to content

random.binomialvariate returns only zero for large n and tiny p with n*p = 1 #158746

Description

@augusto-rehfeldt

Bug report

Bug description:

Documented behaviour: Random.binomialvariate docstring: "The mean (expected value) and variance of the random variable are: E[X] = n * p"; it also defines the result as the number of successes in n independent trials with success probability p.

Expected: A binomial distribution with mean 1 and zero-success probability approximately 0.367879; the empirical mean should be near 1.

Actual: All 4096 integer-seeded samples returned zero; empirical mean 0.0.

import random
import math

n, p, samples = 2**60, 2**-60, 4096
try:
    if not (isinstance(n, int) and n >= 0 and 0 <= p <= 1):
        raise ValueError("input violates documented domain")
    expected = n * p
    values = [random.Random(seed).binomialvariate(n, p)
              for seed in range(samples)]
    actual = sum(values) / samples
    if abs(actual - expected) > 8 * math.sqrt(n * p * (1 - p) / samples):
        print("REFUTATION CONFIRMED:",
              {"n": n, "p": p, "integer_seeds": (0, samples)},
              "actual =", actual, "expected =", expected)
    else:
        print("REFUTATION REJECTED: empirical mean agrees with expectation",
              actual, expected)
except Exception as exc:
    print("REFUTATION REJECTED: could not test:", type(exc).__name__, str(exc))

Output on Python 3.14.6 (Windows-11-10.0.26220-SP0), standard library random:

REFUTATION CONFIRMED: {'n': 1152921504606846976, 'p': 8.673617379884035e-19, 'integer_seeds': (0, 4096)} actual = 0.0 expected = 1.0

This report was found and written by an automated property-testing tool I run (bugforge). The reproducer above was executed and its output is pasted unedited; no person reviewed the report before it was filed. The search script is in https://github.com/augusto-rehfeldt/bugforge-results/tree/main/random-20261003-142339-c3

CPython versions tested on:

3.14

Operating systems tested on:

Windows

Activity

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions