Title: Possible unnecessary OverflowError in random.getrandbits · Issue #133489 · python/cpython · GitHub
Open Graph Title: Possible unnecessary OverflowError in random.getrandbits · Issue #133489 · python/cpython
X Title: Possible unnecessary OverflowError in random.getrandbits · Issue #133489 · python/cpython
Description: Documentation Problem Actual upper bound of a random.Random.randbytes is 2**28 - 1 Following docs and exception message I would expect the limit of 2 ** 32 - 1 as for C 4-bytes integer ## -------------------- bytes methods --------------...
Open Graph Description: Documentation Problem Actual upper bound of a random.Random.randbytes is 2**28 - 1 Following docs and exception message I would expect the limit of 2 ** 32 - 1 as for C 4-bytes integer ## ---------...
X Description: Documentation Problem Actual upper bound of a random.Random.randbytes is 2**28 - 1 Following docs and exception message I would expect the limit of 2 ** 32 - 1 as for C 4-bytes integer ## ---------...
Opengraph URL: https://github.com/python/cpython/issues/133489
X: @github
Domain: github.com
{"@context":"https://schema.org","@type":"DiscussionForumPosting","headline":"Possible unnecessary OverflowError in random.getrandbits","articleBody":"# Documentation\n\n## Problem\nActual upper bound of a `random.Random.randbytes` is `2**28 - 1`\n\nFollowing docs and exception message I would expect the limit of 2 ** 32 - 1 as for C 4-bytes integer \n\n```\n ## -------------------- bytes methods ---------------------\n\n def randbytes(self, n):\n \"\"\"Generate n random bytes.\"\"\"\n return self.getrandbits(n * 8).to_bytes(n, 'little')\n```\n\n\n```\n>>> bytes_string = random.randbytes(2 ** 28)\nTraceback (most recent call last):\n File \"C:\\Program Files\\Python39\\lib\\code.py\", line 90, in runcode\n exec(code, self.locals)\n File \"\u003cinput>\", line 1, in \u003cmodule>\n File \"C:\\Program Files\\Python39\\lib\\random.py\", line 286, in randbytes\n return self.getrandbits(n * 8).to_bytes(n, 'little')\nOverflowError: Python int too large to convert to C int\n>>> bytes_string = random.randbytes(2 ** 28 - 1)\n>>> len(bytes_string)\n268435455\n\n```\n\nReproduced on \nPython 3.9.4 (tags/v3.9.4:1f2e308, Apr 6 2021, 13:40:21) [MSC v.1928 64 bit (AMD64)] on win32\nPython 3.11.2 (main, Jul 19 2024, 12:24:02) [GCC 12.2.0] on linux\n\n\nWill you please point me - if there is a bug in docs/implementation, or this is my misunderstanding only.\nThank you in advance for you patience.\n\n\u003c!-- gh-linked-prs -->\n### Linked PRs\n* gh-133529\n* gh-133658\n* gh-134964\n* gh-134965\n\u003c!-- /gh-linked-prs -->\n","author":{"url":"https://github.com/Nokhrin","@type":"Person","name":"Nokhrin"},"datePublished":"2025-05-06T03:29:27Z","url":"https://github.com/python/cpython/issues/133489"}
| 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:a8e9721a-e1d0-5340-ff7b-40a4b89d9bc2 |
| current-catalog-service-hash | 81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114 |
| request-id | CA82:313081:1205667:17CE8F9:6B01E446 |
| html-safe-nonce | 45f35a692221af240f9e3e289405b5b873b2cef8381024f5e0c7fda2c3fde68a |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiJDQTgyOjMxMzA4MToxMjA1NjY3OjE3Q0U4Rjk6NkIwMUU0NDYiLCJ2aXNpdG9yX2lkIjoiODU4NDc3MDQ1ODI2MjU2MTg2MiIsInJlZ2lvbl9lZGdlIjoiaWFkIiwicmVnaW9uX3JlbmRlciI6ImlhZCJ9 |
| visitor-hmac | 03b17d634a472d216d5fd54ce877d875d76015dd429c23dc2f078492a6235d64 |
| hovercard-subject-tag | issue:3041409673 |
| 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/133489/issue_layout |
| twitter:image | https://opengraph.githubassets.com/47bd773fa4382867e44839b73245e451435dba9c2f08589fb0c9121380ea583d/python/cpython/issues/133489 |
| twitter:card | summary_large_image |
| og:image | https://opengraph.githubassets.com/47bd773fa4382867e44839b73245e451435dba9c2f08589fb0c9121380ea583d/python/cpython/issues/133489 |
| og:image:alt | Documentation Problem Actual upper bound of a random.Random.randbytes is 2**28 - 1 Following docs and exception message I would expect the limit of 2 ** 32 - 1 as for C 4-bytes integer ## ---------... |
| og:image:width | 1200 |
| og:image:height | 600 |
| og:site_name | GitHub |
| og:type | object |
| og:author:username | Nokhrin |
| hostname | github.com |
| expected-hostname | github.com |
| None | cc5dfdd32f62ebd3223cab3af3aa2b268d3605218237326a48a99d1efd89495c |
| 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 | 8c77c9e8439031ad54c75a250694842892f693e1 |
| ui-target | full |
| theme-color | #1e2327 |
| color-scheme | light dark |
| react-app-name | issues |
Links:
Viewport: width=device-width