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 — Command-line interface for the `random` module · Issue #118131 · python/cpython

Command-line interface for the random module #118131

New issue
Copy link
New issue
Copy link
Closed
Closed
Command-line interface for the random module#118131
Copy link
Labels
3.13only security fixesonly security fixesstdlibStandard Library Python modules in the Lib/ directoryStandard Library Python modules in the Lib/ directorytype-featureA feature request or enhancementA feature request or enhancement

Description

@hugovk
hugovk
opened on Apr 21, 2024
Issue body actions

Feature or enhancement

Proposal:

Many stdlib libraries have a simple CLI:

  • https://docs.python.org/3/library/cmdline.html

Some of my favourites:

$ python3 -m http.server
Serving HTTP on :: port 8000 (http://[::]:8000/) ...

$ python3 -m webbrowser https://www.python.org

$ python3 -m uuid
5f73cb76-01d7-4390-8cda-17fe9672a29f

$ python3 -m calendar
                                  2024

      January                   February                   March
Mo Tu We Th Fr Sa Su      Mo Tu We Th Fr Sa Su      Mo Tu We Th Fr Sa Su
 1  2  3  4  5  6  7                1  2  3  4                   1  2  3
[snip]

It would be useful to add a CLI to random to be randomly select a choice (using random.choice):

$ python3 -m random curry "fish n chips" tacos
fish n chips

We can also print a random number:

  • if the input is an integer, print a random integer between 1 and the input (via random.randint)
  • if it's a float, print a random float between 0 and the input (via random.uniform).

For example:

$ python3 -m random a b c
c

$ python3 -m random 6
4

$ python3 -m random 2.5
1.9597922929238814

Also with explicit arguments:

$ python3 -m random --choice a b c
b

$ python3 -m random --integer 6
6

$ python3 -m random --float 2.5
1.1778540129562416

$ python3 -m random --float 6
1.4311142568750403

This isn't a Python-specific tool, like pdb or pickletools, but a generally useful tool like http.server and uuid. I can't find an existing cross-platform tool for this. Linux has shuf to return a random choice, but it's not natively available in macOS (it's part of a Homebrew package) or Windows (uutils can be installed using Scoop). There's also a num-utils package for Unix to print a random integer.

For me, just the random choice is the most important, but I can see at least picking a random integer is also useful, like a dice throw.

Currently, python -m random generates some sample test output; if we still need it I propose moving it to python -m random --test.

Has this already been discussed elsewhere?

I have already discussed this feature proposal on Discourse

Links to previous discussion of this feature:

  • https://discuss.python.org/t/command-line-interface-for-the-random-module/51304
  • https://mastodon.social/@hugovk/112292755013247928

Linked PRs

  • gh-118131: Command-line interface for the random module #118132
Reacted by Kirill Podoprigora, sobolevn, blhsing, An Long, iforapsy and Erlend E. Aasland

Activity

  1. hugovk
    added
    type-featureA feature request or enhancementA feature request or enhancement
    stdlibStandard Library Python modules in the Lib/ directoryStandard Library Python modules in the Lib/ directory
    3.13only security fixesonly security fixes
    on Apr 21, 2024
  2. merwok commented on Apr 23, 2024

    @merwok
    merwok
    on Apr 23, 2024
    Member
    More actions

    I think this needs stronger support from python-dev on discuss before being added.

  3. hugovk
    added a commit that references this issue on May 5, 2024

    gh-118131: Command-line interface for the `random` module (#118132)

    3b32575
  4. SonicField
    added a commit that references this issue on May 8, 2024

    pythongh-118131: Command-line interface for the `random` module (pyth…

    73cee61
  5. picnixz commented on Jun 22, 2024

    @picnixz
    picnixz
    on Jun 22, 2024
    Member
    More actions

    @hugovk Do you think this issue can be closed?

    Reacted by Hugo van Kemenade
  6. hugovk
    closed this as completedon Jun 22, 2024
  7. andersk
    added 2 commits that reference this issue on Sep 11, 2024

    pythongh-123968: Fix lower bound for `python -m random --float`

    98fb826

    pythongh-123968: Fix lower bound for `python -m random --float`

    14e414a
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

    3.13only security fixesonly security fixesstdlibStandard Library Python modules in the Lib/ directoryStandard Library Python modules in the Lib/ directorytype-featureA feature request or enhancementA feature request or enhancement

    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.