GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — `python -m random N` prints a float for an integer N <= 0 · Issue #161452 · python/cpython

python -m random N prints a float for an integer N <= 0 #161452

New issue
Copy link
New issue
Copy link
Open
Open
python -m random N prints a float for an integer N <= 0#161452
Copy link
Labels
stdlibStandard Library Python modules in the Lib/ directoryStandard Library Python modules in the Lib/ directorytype-bugAn unexpected behavior, bug, or errorAn unexpected behavior, bug, or error

Description

@andresdelfino
andresdelfino
opened on Nov 5, 2026
Issue body actions

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:

$ python -m random -i 0
Traceback (most recent call last):
  ...
ValueError: empty range in randint(1, 0)
$ python -m random 0
0.0
$ python -m random -5
-2.8133945085995364

main() calls randint() inside the same try block that converts the input with int():

try:
    # Is it an integer?
    val = int(val)
    return randint(1, val)
except ValueError:
    try:
        # Is it a float?
        val = float(val)
        return uniform(0, val)

so 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.

CPython versions tested on:

3.14, 3.15, CPython main branch

Operating systems tested on:

Linux

Linked PRs

  • gh-161452: Fix python -m random with a non-positive integer input #161453

Activity

  1. skirpichev
    added
    stdlibStandard Library Python modules in the Lib/ directoryStandard Library Python modules in the Lib/ directory
    type-featureA feature request or enhancementA feature request or enhancement
    type-bugAn unexpected behavior, bug, or errorAn unexpected behavior, bug, or error
    and removed
    type-featureA feature request or enhancementA feature request or enhancement
    on Nov 5, 2026
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

    stdlibStandard Library Python modules in the Lib/ directoryStandard Library Python modules in the Lib/ directorytype-bugAn unexpected behavior, bug, or errorAn unexpected behavior, bug, or error

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions

      You are visitor 01402503 — GeezerVison does not store the pages you look up.
      Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.