Skip to content

Command-line interface for the random module #118131

Closed
@hugovk

Description

@hugovk

Feature or enhancement

Proposal:

Many stdlib libraries have a simple CLI:

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:

Linked PRs

Metadata

Metadata

Assignees

No one assigned

    Labels

    3.13bugs and security fixesstdlibPython modules in the Lib dirtype-featureA feature request or enhancement

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions