Repository navigation
Description
Bug report
Bug description:
Documented behaviour: Python random documentation, choices(): “If a weights sequence is specified, selections are made according to the relative weights.” Equal relative weights imply equal selection probabilities.
Expected: Equal selection probabilities, with approximately 50000 selections per element; scaling to [1.0, 1.0] should preserve the distribution.
Actual: 25146 selections of 'a' and 74854 of 'b', consistent with probabilities approximately 1/4 and 3/4.
import random, math
from fractions import Fraction
p, w, k, seed = ['a', 'b'], [2**-1074]*2, 100000, 0
data = dict(population=p, weights=w, k=k, seed=seed)
if not (len(p) == len(w) and all(math.isfinite(x) and x > 0 for x in w)
and math.isfinite(sum(w)) and sum(w) > 0):
print("REFUTATION REJECTED:", "invalid input", data)
else:
q = [Fraction(x) for x in w]
probabilities = [x / sum(q) for x in q]
draws = random.Random(seed).choices(p, weights=w, k=k)
actual = {x: draws.count(x) for x in p}
expected = {x: dict(probability=str(t), mean=k*float(t),
tolerance=10*math.sqrt(k*float(t)*(1-float(t))))
for x, t in zip(p, probabilities)}
broken = any(abs(actual[x] - expected[x]['mean']) > expected[x]['tolerance']
for x in p)
if broken:
print("REFUTATION CONFIRMED:", data, "actual:", actual, "expected:", expected)
else:
print("REFUTATION REJECTED:", "counts within 10 standard deviations",
data, "actual:", actual, "expected:", expected)Output on Python 3.14.6 (Windows-11-10.0.26220-SP0), standard library random:
REFUTATION CONFIRMED: {'population': ['a', 'b'], 'weights': [5e-324, 5e-324], 'k': 100000, 'seed': 0} actual: {'a': 25146, 'b': 74854} expected: {'a': {'probability': '1/2', 'mean': 50000.0, 'tolerance': 1581.1388300841897}, 'b': {'probability': '1/2', 'mean': 50000.0, 'tolerance': 1581.1388300841897}}
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-202849-c3
CPython versions tested on:
3.14
Operating systems tested on:
Windows
Activity
Link random-20261003-202849-c3 to python/cpython#158748
ZeroIntensity commentedon Oct 4, 2026
This is not allowed. Please stop flooding our issue tracker and instead compile a gist with your findings, and then track them with a single issue. I am going to be closing these to avoid the spam.
augusto-rehfeldt commentedon Oct 4, 2026
Sorry about this. My tool filed these in bulk and I should not have let it; it is now limited to one open report per project. As you suggested, everything is compiled in one place and tracked in #158786. (Drafted with an AI assistant.)