Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Command-line interface for the random module #118131

Closed
hugovk opened this issue Apr 21, 2024 · 2 comments
Closed

Command-line interface for the random module #118131

hugovk opened this issue Apr 21, 2024 · 2 comments
Labels
3.13 bugs and security fixes stdlib Python modules in the Lib dir type-feature A feature request or enhancement

Comments

@hugovk
Copy link
Member

hugovk commented Apr 21, 2024

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

@hugovk hugovk added type-feature A feature request or enhancement stdlib Python modules in the Lib dir 3.13 bugs and security fixes labels Apr 21, 2024
@merwok
Copy link
Member

merwok commented Apr 23, 2024

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

@picnixz
Copy link
Contributor

picnixz commented Jun 22, 2024

@hugovk Do you think this issue can be closed?

@hugovk hugovk closed this as completed Jun 22, 2024
andersk added a commit to andersk/cpython that referenced this issue Sep 11, 2024
`python -m random --float N` incorrectly used a range between 1 and N
of length N - 1.  Fix it to use a range between 0 and N of length N,
as originally specified in python#118131, and as everyone will expect.

Fixes python#123968.

Signed-off-by: Anders Kaseorg <andersk@mit.edu>
andersk added a commit to andersk/cpython that referenced this issue Sep 11, 2024
`python -m random --float N` incorrectly used a range between 1 and N
of length N - 1.  Fix it to use a range between 0 and N of length N,
as originally specified in python#118131, and as everyone will expect.

Fixes python#123968.

Signed-off-by: Anders Kaseorg <andersk@mit.edu>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
3.13 bugs and security fixes stdlib Python modules in the Lib dir type-feature A feature request or enhancement
Projects
None yet
Development

No branches or pull requests

3 participants