# Command-line interface for the \`random\` module

**URL:** <https://discuss.python.org/t/command-line-interface-for-the-random-module/51304>\
**Category:** Ideas\
**Created:** [April 18, 2024, 2:32pm UTC](https://discuss.python.org/t/command-line-interface-for-the-random-module/51304 "2024-04-18T14:32:00Z")\
**Posts on this page:** 20\
**Page:** 1

<div class="post-metadata">

**Author:** ![hugovk](https://sea2.discourse-cdn.com/flex002/user_avatar/discuss.python.org/hugovk/32/14505_2.png) [@hugovk](https://discuss.python.org/u/hugovk)\
**Post date:** [April 18, 2024, 2:32pm UTC](https://discuss.python.org/t/command-line-interface-for-the-random-module/51304/1 "2024-04-18T14:32:00Z")

</div>

Many stdlib libraries have a simple CLI:

- [Modules command-line interface (CLI) — Python 3.12.3 documentation](https://docs.python.org/3/library/cmdline.html)

Some of my favourites:

```console
$ 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]

```

I think it would be useful to add a CLI to `random` to be randomly select a choice (using [`random.choice`](https://docs.python.org/3/library/random.html#random.choice)):

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

```

This wouldn’t be a Python-specific tool, like `pdb` or `pickletools`, but a generally useful tool like the ones above. I also can’t think of an existing CLI tool to do this (cross-platform or not).

We could add CLI arguments: if the input is an integer, return a random integer between 1 and the input (via [`random.randint`](https://docs.python.org/3/library/random.html#random.randint)), if it’s a float, return a raondom float between 0 and the input (via [`random.uniform`](https://docs.python.org/3/library/random.html#random.uniform)).

For example:

```console
$ python3 -m random a b c
b

$ python3 -m random 4
1

$ python3 -m random 2.5
1.5817380334578792

```

And we could also have explicit arguments:

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

$ python3 -m random --integer 4
3

$ python3 -m random --float 2.5
2.2419142269846186

$ python3 -m random --integer 2
1

$ python3 -m random --float 2
1.4463835577108637

```

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

I’ve implemented this, and it’s about 60 lines of code, and 40 lines of tests.

Currently, `python -m random` generates some sample test output. We could remove this, or if we still want it, move it to `python -m random --test`.

What do you think?

(See also the [`re` CLI proposal](https://discuss.python.org/t/command-line-interface-for-the-re-module/31819).)

---

<div class="post-metadata">

**Author:** ![effigies](https://sea2.discourse-cdn.com/flex002/user_avatar/discuss.python.org/effigies/32/35835_2.png) [@effigies](https://discuss.python.org/u/effigies)\
**Post date:** [April 18, 2024, 3:04pm UTC](https://discuss.python.org/t/command-line-interface-for-the-random-module/51304/2 "2024-04-18T15:04:51Z")

</div>

I like it! It would be good to be able to pipe from stdin, e.g.:

```console
$ python -m random < raffle_entries
Entrant N <entrantN@mailserver.com>

```

---

<div class="post-metadata">

**Author:** ![bschubert](https://sea2.discourse-cdn.com/flex002/user_avatar/discuss.python.org/bschubert/32/26920_2.png) [@bschubert](https://discuss.python.org/u/bschubert)\
**Post date:** [April 18, 2024, 3:42pm UTC](https://discuss.python.org/t/command-line-interface-for-the-random-module/51304/3 "2024-04-18T15:42:17Z")

</div>

> [@hugovk](#):
>
> I also can’t think of an existing CLI tool to do this (cross-platform or not).

At least on Unix system, there’s coreutils [`shuf`](https://man7.org/linux/man-pages/man1/shuf.1.html), which implements random choices and random integer generation:

```python
$ shuf -n 1 -e a b c
b

$ shuf -n 1 -i 0-4
3

$ python3 -c 'import this' | shuf -n 1
If the implementation is easy to explain, it may be a good idea.

```

---

<div class="post-metadata">

**Author:** ![hugovk](https://sea2.discourse-cdn.com/flex002/user_avatar/discuss.python.org/hugovk/32/14505_2.png) [@hugovk](https://discuss.python.org/u/hugovk)\
**Post date:** [April 18, 2024, 4:22pm UTC](https://discuss.python.org/t/command-line-interface-for-the-random-module/51304/4 "2024-04-18T16:22:04Z")

</div>

Thanks, at least `shuf` isn’t natively available on macOS (it’s part of a Homebrew package), and probably not on Windows?

---

<div class="post-metadata">

**Author:** ![pf\_moore](https://sea2.discourse-cdn.com/flex002/user_avatar/discuss.python.org/pf_moore/32/35_2.png) [@pf\_moore](https://discuss.python.org/u/pf_moore)\
**Post date:** [April 18, 2024, 6:21pm UTC](https://discuss.python.org/t/command-line-interface-for-the-random-module/51304/5 "2024-04-18T18:21:20Z")

</div>

Not natively on Windows, but it’s available via [uutils](https://github.com/uutils/coreutils) which can be installed using [Scoop](https://scoop.sh/)

+1 on having it as a CLI for the random module, though.

---

<div class="post-metadata">

**Author:** ![kknechtel](https://avatars.discourse-cdn.com/v4/letter/k/e47c2d/32.png) [@kknechtel](https://discuss.python.org/u/kknechtel)\
**Post date:** [April 18, 2024, 6:35pm UTC](https://discuss.python.org/t/command-line-interface-for-the-random-module/51304/6 "2024-04-18T18:35:53Z")

</div>

I’m also +1 on this. Being able to do something without Python isn’t IMO a reason why Python shouldn’t do it. After all, `python3 -m webbrowser https://www.python.org` could just as easily be, say, `firefox https://www.python.org`.

---

<div class="post-metadata">

**Author:** ![storchaka](https://sea2.discourse-cdn.com/flex002/user_avatar/discuss.python.org/storchaka/32/217_2.png) [@storchaka](https://discuss.python.org/u/storchaka)\
**Post date:** [April 18, 2024, 10:13pm UTC](https://discuss.python.org/t/command-line-interface-for-the-random-module/51304/7 "2024-04-18T22:13:03Z")

</div>

There is also a tool in the num-utils package:  
[https://suso.suso.org/programs/num-utils/man1/random.html](https://suso.suso.org/programs/num-utils/man1/random.html)

---

<div class="post-metadata">

**Author:** ![blhsing](https://sea2.discourse-cdn.com/flex002/user_avatar/discuss.python.org/blhsing/32/25812_2.png) [@blhsing](https://discuss.python.org/u/blhsing)\
**Post date:** [April 19, 2024, 3:57am UTC](https://discuss.python.org/t/command-line-interface-for-the-random-module/51304/8 "2024-04-19T03:57:18Z")

</div>

+1, and do the same to the [`secrets`](https://docs.python.org/3/library/secrets.html) module, without support for float but with support for token generation.

---

<div class="post-metadata">

**Author:** ![hugovk](https://sea2.discourse-cdn.com/flex002/user_avatar/discuss.python.org/hugovk/32/14505_2.png) [@hugovk](https://discuss.python.org/u/hugovk)\
**Post date:** [April 21, 2024, 4:04pm UTC](https://discuss.python.org/t/command-line-interface-for-the-random-module/51304/9 "2024-04-21T16:04:21Z")

</div>

Thanks everyone for the feedback! I’ve opened an issue:

- [python/cpython#118131](https://github.com/python/cpython/issues/118131)

And PR:

- [python/cpython#118132](https://github.com/python/cpython/pull/118132)

---

<div class="post-metadata">

**Author:** ![graingert](https://sea2.discourse-cdn.com/flex002/user_avatar/discuss.python.org/graingert/32/26847_2.png) [@graingert](https://discuss.python.org/u/graingert)\
**Post date:** [April 21, 2024, 4:23pm UTC](https://discuss.python.org/t/command-line-interface-for-the-random-module/51304/10 "2024-04-21T16:23:17Z")

</div>

I think it should default to secure random

---

<div class="post-metadata">

**Author:** ![merwok](https://sea2.discourse-cdn.com/flex002/user_avatar/discuss.python.org/merwok/32/29_2.png) [@merwok](https://discuss.python.org/u/merwok)\
**Post date:** [April 21, 2024, 10:04pm UTC](https://discuss.python.org/t/command-line-interface-for-the-random-module/51304/11 "2024-04-21T22:04:16Z")

</div>

Personally I don’t think the stdlib shoud also be the provider of basic command-line utilities spread out over many modules as more-or-less official and supported \_\_main\_\_ blocks.

These can be provided by a coreutils package installed in the OS, or the shell itself (sh-like like busybox, or inventing its own language like xonsh or ipython), or one PyPI package.

---

<div class="post-metadata">

**Author:** ![merwok](https://sea2.discourse-cdn.com/flex002/user_avatar/discuss.python.org/merwok/32/29_2.png) [@merwok](https://discuss.python.org/u/merwok)\
**Post date:** [April 21, 2024, 10:06pm UTC](https://discuss.python.org/t/command-line-interface-for-the-random-module/51304/12 "2024-04-21T22:06:04Z")

</div>

> [@kknechtel](#):
>
> After all, `python3 -m webbrowser https://www.python.org` could just as easily be, say, `firefox https://www.python.org`.

This is not a good example, as webbrowser is useful there to pick the correct browser for the context.  
So it’s similar to exo-open or xdg-open, which are not standad or ubiquitous, hence the value of webbrowser (and its easter egg CLI).

---

<div class="post-metadata">

**Author:** ![Constantin1489](https://sea2.discourse-cdn.com/flex002/user_avatar/discuss.python.org/constantin1489/32/19417_2.png) [@Constantin1489](https://discuss.python.org/u/Constantin1489)\
**Post date:** [April 23, 2024, 12:59pm UTC](https://discuss.python.org/t/command-line-interface-for-the-random-module/51304/13 "2024-04-23T12:59:15Z")

</div>

Hi, I didn’t test the code but, does the `-h` or `--help` print the warning of [random — Generate pseudo-random numbers — Python 3.12.3 documentation](https://docs.python.org/3/library/random.html) ?

---

<div class="post-metadata">

**Author:** ![hugovk](https://sea2.discourse-cdn.com/flex002/user_avatar/discuss.python.org/hugovk/32/14505_2.png) [@hugovk](https://discuss.python.org/u/hugovk)\
**Post date:** [April 23, 2024, 7:01pm UTC](https://discuss.python.org/t/command-line-interface-for-the-random-module/51304/14 "2024-04-23T19:01:55Z")

</div>

```console
$ ./python.exe -m random --help
usage: random.py [-h] [-c CHOICE [CHOICE ...] | -i N | -f N] [input ...]

positional arguments:
  input if no options given, output depends on the input
                            string or multiple: same as --choice
                            integer: same as --integer
                            float: same as --float

options:
  -h, --help show this help message and exit
  -c, --choice CHOICE [CHOICE ...]
                        print a random choice
  -i, --integer N print a random integer between 1 and N inclusive
  -f, --float N print a random floating point number between 1 and N inclusive

```

---

<div class="post-metadata">

**Author:** ![matteoguadrini](https://sea2.discourse-cdn.com/flex002/user_avatar/discuss.python.org/matteoguadrini/32/18687_2.png) [@matteoguadrini](https://discuss.python.org/u/matteoguadrini)\
**Post date:** [April 25, 2024, 7:32pm UTC](https://discuss.python.org/t/command-line-interface-for-the-random-module/51304/15 "2024-04-25T19:32:13Z")

</div>

+1 to have a random module cli!  
This is a very wonderful idea!

---

<div class="post-metadata">

**Author:** ![csm10495](https://sea2.discourse-cdn.com/flex002/user_avatar/discuss.python.org/csm10495/32/9731_2.png) [@csm10495](https://discuss.python.org/u/csm10495)\
**Post date:** [April 26, 2024, 1:54am UTC](https://discuss.python.org/t/command-line-interface-for-the-random-module/51304/16 "2024-04-26T01:54:50Z")

</div>

It might be nice if there was a `-s/--start` param to change the start from 1 to … what the user gives (still defaulting to 1 is fine).

---

<div class="post-metadata">

**Author:** ![erlendaasland](https://sea2.discourse-cdn.com/flex002/user_avatar/discuss.python.org/erlendaasland/32/4378_2.png) [@erlendaasland](https://discuss.python.org/u/erlendaasland)\
**Post date:** [April 29, 2024, 9:35am UTC](https://discuss.python.org/t/command-line-interface-for-the-random-module/51304/17 "2024-04-29T09:35:54Z")

</div>

I like this. The implementation is trivial, so there is not much of a maintenance cost. Count me +1.

I agree that we don’t need to add a CLI to _every_ stdlib module, and of course we don’t need to duplicate `coreutils` or other system utility packages (I don’t think anyone is actually proposing that either), but I find _this_ proposal appealing (as I also do with Serhiy’s proposal to add a `re` CLI, and the existing `json.tool` CLI).

---

<div class="post-metadata">

**Author:** ![vstinner](https://sea2.discourse-cdn.com/flex002/user_avatar/discuss.python.org/vstinner/32/15130_2.png) [@vstinner](https://discuss.python.org/u/vstinner)\
**Post date:** [April 29, 2024, 1:00pm UTC](https://discuss.python.org/t/command-line-interface-for-the-random-module/51304/18 "2024-04-29T13:00:17Z")

</div>

I would be interested to have `python -m random --bytes 4` to get 4 random bytes written to stdout.

> [@hugovk](#):
>
> ```python
> -i, --integer N print a random integer between 1 and N inclusive
> -f, --float N print a random floating point number between 1 and N inclusive
> 
> ```

IMO making the assumption of 1 is too specific. I suggest to pass a range instead:

- `python -m random --integer 1 6`: dice, [1; 6] range (inclusive)
- `python -m random --integer 0 1`: boolean, [0; 1] range (inclusive)
- `python -m random --float -2 3.5`: [-2.0; +3.5] range (inclusive)

* * *

I’m not sure if `--choice` CLI is the best CLI. Maybe using sub-commands would create a better CLI to make arguments specific per command. Example:

```python
# [1; 6] inclusive
python -m random integer 6

# [1; 6] inclusive; explicit start-stop CLI
python -m random integer 1 6

# [-2; 3.5] range, inclusive
python -m random float -2 3.5

# [-2; 3.5( range, include -2 but exclude 3.5
python -m random float -2 3.5 --exclude-right

python -m random choice Victor Hugo Brian

# read choices from stdin
python -m random choice --stdin < input  

python -m random bytes 4
# Encode bytes as hexadecimal with spaces,
# such as "0xD3 0x5A 0x90 0xFE"
python -m random bytes 4 --hex

```

I don’t propose to add all options. I just want to show that there might be more needs in the future, and propose `--integer` CLI might be too limited.

---

<div class="post-metadata">

**Author:** ![rhettinger](https://sea2.discourse-cdn.com/flex002/user_avatar/discuss.python.org/rhettinger/32/1129_2.png) [@rhettinger](https://discuss.python.org/u/rhettinger)\
**Post date:** [April 30, 2024, 4:33am UTC](https://discuss.python.org/t/command-line-interface-for-the-random-module/51304/19 "2024-04-30T04:33:06Z")

</div>

> [@merwok](#):
>
> Personally I don’t think the stdlib shoud also be the provider of basic command-line utilities spread out over many modules as more-or-less official and supported **main** blocks.

Éric, people should pay more attention to your comment. It is exactly correct and it seems like it is being casually dismissed or ignored.

We had discussions on this before and the outcome of the discussion was that we are in the language and library business, not in the command-line tool business. Really, that is something best left to packages on PyPI and not something to put in our standard distribution.

Raymond

* * *

[RH]

> Guido, would you can to opine on this? Every now and then we get a request to make command-line utilities out of tools in the standard library. Whether we should or not depends on whether the standard library intends to be primarily a library for Python code or whether it is also about providing general purpose toolkits that might be helpful in a non-unix environment. We’ve has some of these that have met with success (for example, timeit, json.tool, and SimpleHTTPServer) and others that were just a waste or a were a pale shadow of their full featured Unix counterparts (or left to rot in the Tools directory). If we go further down this road, it would be nice for you to lay out the ground rules for what kind of command line tools would be acceptable, how stable their API would be, and whether they should be separated from the module itself. Do you even want to be in the business of offering command-line APIs that duplicate commonly available Unix tools?

[GvR]

> I prefer not to go down this road. The modules that do this where I use it are typically Python specific, e.g. pdb or timeit. In the past we sometimes had little main() functions in modules for testing but I think we have better ways to test modules these days.

---

<div class="post-metadata">

**Author:** ![rhettinger](https://sea2.discourse-cdn.com/flex002/user_avatar/discuss.python.org/rhettinger/32/1129_2.png) [@rhettinger](https://discuss.python.org/u/rhettinger)\
**Post date:** [April 30, 2024, 4:39am UTC](https://discuss.python.org/t/command-line-interface-for-the-random-module/51304/20 "2024-04-30T04:39:29Z")

</div>

FWIW the last time CLI I remember was credible than the currently under consideration. Still, it was rejected:

```python
$ python -m hashlib md5 somefile.txt
$ d985d0ea551c1253c2305140c583d11f

```

[Next page](https://discuss.python.org/t/command-line-interface-for-the-random-module/51304.md?page=2)
