Title: random.choices produces strongly biased results for equal subnormal weights · Issue #158748 · python/cpython · GitHub
Open Graph Title: random.choices produces strongly biased results for equal subnormal weights · Issue #158748 · python/cpython
X Title: random.choices produces strongly biased results for equal subnormal weights · Issue #158748 · python/cpython
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 probabili...
Open Graph 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...
X 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...
Opengraph URL: https://github.com/python/cpython/issues/158748
X: @github
Domain: github.com
{"@context":"https://schema.org","@type":"DiscussionForumPosting","headline":"random.choices produces strongly biased results for equal subnormal weights","articleBody":"# Bug report\r\n\r\n### Bug description:\r\n\r\n**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.\r\n\r\n**Expected:** Equal selection probabilities, with approximately 50000 selections per element; scaling to [1.0, 1.0] should preserve the distribution.\r\n\r\n**Actual:** 25146 selections of 'a' and 74854 of 'b', consistent with probabilities approximately 1/4 and 3/4.\r\n\r\n```python\r\nimport random, math\r\nfrom fractions import Fraction\r\n\r\np, w, k, seed = ['a', 'b'], [2**-1074]*2, 100000, 0\r\ndata = dict(population=p, weights=w, k=k, seed=seed)\r\nif not (len(p) == len(w) and all(math.isfinite(x) and x > 0 for x in w)\r\n and math.isfinite(sum(w)) and sum(w) > 0):\r\n print(\"REFUTATION REJECTED:\", \"invalid input\", data)\r\nelse:\r\n q = [Fraction(x) for x in w]\r\n probabilities = [x / sum(q) for x in q]\r\n draws = random.Random(seed).choices(p, weights=w, k=k)\r\n actual = {x: draws.count(x) for x in p}\r\n expected = {x: dict(probability=str(t), mean=k*float(t),\r\n tolerance=10*math.sqrt(k*float(t)*(1-float(t))))\r\n for x, t in zip(p, probabilities)}\r\n broken = any(abs(actual[x] - expected[x]['mean']) > expected[x]['tolerance']\r\n for x in p)\r\n if broken:\r\n print(\"REFUTATION CONFIRMED:\", data, \"actual:\", actual, \"expected:\", expected)\r\n else:\r\n print(\"REFUTATION REJECTED:\", \"counts within 10 standard deviations\",\r\n data, \"actual:\", actual, \"expected:\", expected)\r\n```\r\n\r\nOutput on Python 3.14.6 (Windows-11-10.0.26220-SP0), standard library `random`:\r\n\r\n```\r\nREFUTATION 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}}\r\n```\r\n\r\nThis report was found and written by an automated property-testing tool I run ([bugforge](https://github.com/augusto-rehfeldt/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\r\n\r\n### CPython versions tested on:\r\n\r\n3.14\r\n\r\n### Operating systems tested on:\r\n\r\nWindows\r\n","author":{"url":"https://github.com/augusto-rehfeldt","@type":"Person","name":"augusto-rehfeldt"},"datePublished":"2026-10-04T01:04:33Z","url":"https://github.com/python/cpython/issues/158748"}
| react-profiling | 0 |
| react-import-map | react,react-dom,react-dom/client,react-dom/profiling,react-is,react/compiler-runtime,react/jsx-dev-runtime,react/jsx-runtime,scheduler@b8488ee2c322 |
| route-pattern | /_view_fragments/issues/show/:user_id/:repository/:id/issue_layout(.:format) |
| route-controller | voltron_issues_fragments |
| route-action | issue_layout |
| fetch-nonce | v2:7b51fc47-f74d-9c6b-737a-9f2abc51c993 |
| current-catalog-service-hash | 81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114 |
| request-id | 8E52:34CAB:40827A9:5267FEE:6B022F57 |
| html-safe-nonce | 1ea81a921b3e67c1b1f6d3b7eede15cddbed4921e84447660befab07dcd475f7 |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiI4RTUyOjM0Q0FCOjQwODI3QTk6NTI2N0ZFRTo2QjAyMkY1NyIsInZpc2l0b3JfaWQiOiIyMzA5NDE1NzI4MDE0NTY5MzAzIiwicmVnaW9uX2VkZ2UiOiJpYWQiLCJyZWdpb25fcmVuZGVyIjoiaWFkIn0= |
| visitor-hmac | c792593eaa73590cb24fe753488ba6fb67791a31687561bdb7da7f181e56302f |
| hovercard-subject-tag | issue:5695093120 |
| github-keyboard-shortcuts | repository,issues,copilot |
| google-site-verification | Apib7-x98H0j5cPqHWwSMm6dNU4GmODRoqxLiDzdx9I |
| octolytics-url | https://collector.github.com/github/collect |
| analytics-location | / |
| fb:app_id | 1401488693436528 |
| apple-itunes-app | app-id=1477376905, app-argument=https://github.com/_view_fragments/issues/show/python/cpython/158748/issue_layout |
| twitter:image | https://opengraph.githubassets.com/f3024940d8764536a95e696105b3ea8fe96e6830f49663dac64904979bc47970/python/cpython/issues/158748 |
| twitter:card | summary_large_image |
| og:image | https://opengraph.githubassets.com/f3024940d8764536a95e696105b3ea8fe96e6830f49663dac64904979bc47970/python/cpython/issues/158748 |
| og:image:alt | 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... |
| og:image:width | 1200 |
| og:image:height | 600 |
| og:site_name | GitHub |
| og:type | object |
| og:author:username | augusto-rehfeldt |
| hostname | github.com |
| expected-hostname | github.com |
| None | 87e774ee7c6b3bee0490394e4d3e57ec9dbc956f74b87ea685dc8996c1676456 |
| turbo-cache-control | no-preview |
| go-import | github.com/python/cpython git https://github.com/python/cpython.git |
| octolytics-dimension-user_id | 1525981 |
| octolytics-dimension-user_login | python |
| octolytics-dimension-repository_id | 81598961 |
| octolytics-dimension-repository_nwo | python/cpython |
| octolytics-dimension-repository_public | true |
| octolytics-dimension-repository_is_fork | false |
| octolytics-dimension-repository_network_root_id | 81598961 |
| octolytics-dimension-repository_network_root_nwo | python/cpython |
| turbo-body-classes | logged-out env-production page-responsive |
| disable-turbo | false |
| browser-stats-url | https://api.github.com/_private/browser/stats |
| browser-errors-url | https://api.github.com/_private/browser/errors |
| release | 423dbb54c5e7bfecc838eb387c1435eb3d0e760d |
| ui-target | full |
| theme-color | #1e2327 |
| color-scheme | light dark |
| react-app-name | issues |
Links:
Viewport: width=device-width