Title: `python -m random N` prints a float for an integer N <= 0 · Issue #161452 · python/cpython · GitHub
Open Graph Title: `python -m random N` prints a float for an integer N <= 0 · Issue #161452 · python/cpython
X Title: `python -m random N` prints a float for an integer N <= 0 · Issue #161452 · python/cpython
Description: Bug report Bug description: The command-line interface of random documents that, when no option is given, an integer input is handled "same as --integer". For an integer that is zero or negative, it prints a random float instead: $ pytho...
Open Graph Description: Bug report Bug description: The command-line interface of random documents that, when no option is given, an integer input is handled "same as --integer". For an integer that is zero or negative, i...
X Description: Bug report Bug description: The command-line interface of random documents that, when no option is given, an integer input is handled "same as --integer". For an integer that is zero or n...
Opengraph URL: https://github.com/python/cpython/issues/161452
X: @github
Domain: github.com
{"@context":"https://schema.org","@type":"DiscussionForumPosting","headline":"`python -m random N` prints a float for an integer N \u003c= 0","articleBody":"### Bug report\n\n### Bug description:\n\nThe command-line interface of `random` documents that, when no option is given, an integer input is handled \"same as `--integer`\". For an integer that is zero or negative, it prints a random float instead:\n\n```shell\n$ python -m random -i 0\nTraceback (most recent call last):\n ...\nValueError: empty range in randint(1, 0)\n$ python -m random 0\n0.0\n$ python -m random -5\n-2.8133945085995364\n```\n\n`main()` calls `randint()` inside the same `try` block that converts the input with `int()`:\n\n```python\ntry:\n # Is it an integer?\n val = int(val)\n return randint(1, val)\nexcept ValueError:\n try:\n # Is it a float?\n val = float(val)\n return uniform(0, val)\n```\n\nso the `ValueError` that `randint()` raises for an empty range is taken to mean that the input is not an integer, and the input is then handled as a float. Keeping only the `int()` call inside the `try` block makes `python -m random N` behave like `python -m random --integer N` for every integer.\n\n### CPython versions tested on:\n\n3.14, 3.15, CPython main branch\n\n### Operating systems tested on:\n\nLinux\n\n\n\u003c!-- gh-linked-prs -->\n### Linked PRs\n* gh-161453\n* gh-161942\n* gh-161943\n\u003c!-- /gh-linked-prs -->\n","author":{"url":"https://github.com/andresdelfino","@type":"Person","name":"andresdelfino"},"datePublished":"2026-11-05T22:26:35Z","url":"https://github.com/python/cpython/issues/161452"}
| 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:c517bd30-23bb-e812-e6d5-5f76c6647c84 |
| current-catalog-service-hash | 81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114 |
| request-id | A982:842EC:345543E:45C34C1:6B020294 |
| html-safe-nonce | 57834e78b65adfc018443fc8ff8a76f69930a400e29636b3de42455da50099ba |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiJBOTgyOjg0MkVDOjM0NTU0M0U6NDVDMzRDMTo2QjAyMDI5NCIsInZpc2l0b3JfaWQiOiIxODc3NjY0ODUzNTQ4NzI4OTgwIiwicmVnaW9uX2VkZ2UiOiJpYWQiLCJyZWdpb25fcmVuZGVyIjoiaWFkIn0= |
| visitor-hmac | b221c8d6e2ea1cdbcaafd341178c76f490a00a8510b770a9680e6b0ee298fefa |
| hovercard-subject-tag | issue:6232777016 |
| 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/161452/issue_layout |
| twitter:image | https://opengraph.githubassets.com/7d961cbbab92a1b003606fcfe7f88b5e51aa91992ac680afd629b0af32a2642c/python/cpython/issues/161452 |
| twitter:card | summary_large_image |
| og:image | https://opengraph.githubassets.com/7d961cbbab92a1b003606fcfe7f88b5e51aa91992ac680afd629b0af32a2642c/python/cpython/issues/161452 |
| og:image:alt | Bug report Bug description: The command-line interface of random documents that, when no option is given, an integer input is handled "same as --integer". For an integer that is zero or negative, i... |
| og:image:width | 1200 |
| og:image:height | 600 |
| og:site_name | GitHub |
| og:type | object |
| og:author:username | andresdelfino |
| 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