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

python -m random --float N should pick a float between 0 and N, not 1 and N #123968

Closed
andersk opened this issue Sep 11, 2024 · 2 comments
Closed
Labels
3.13 bugs and security fixes 3.14 new features, bugs and security fixes release-blocker stdlib Python modules in the Lib dir type-bug An unexpected behavior, bug, or error

Comments

@andersk
Copy link
Contributor

andersk commented Sep 11, 2024

Bug report

Bug description:

In the specification for the new python -m random CLI for 3.13 (#118131), the proposed behavior was

if it's a float, print a random float between 0 and the input (via random.uniform).

However, what was actually implemented (#118132, cc @hugovk) was

$ python -m random --help
[…]
  -f, --float N         print a random floating-point number between 1 and N inclusive
$ python -m random 2.0
1.883974829952927
$ python -m random 2.0
1.034610672623156
$ python -m random 2.0
1.3676261878147473
$ python -m random 2.0
1.1875340810783404
$ python -m random 2.0
1.6148479875565644
$ python -m random 1.0
1.0

This is surprising and not helpful. Everyone will expect a range of length N starting from 0.0, not a range of length N - 1.0 starting from 1.0.

(Note that this is completely distinct from the debate about whether it’s more natural to select integers from [1, …, N] or [0, …, N - 1], as at least those are both ranges of length N.)

CPython versions tested on:

3.13

Operating systems tested on:

Linux

Linked PRs

@andersk andersk added the type-bug An unexpected behavior, bug, or error label Sep 11, 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 <[email protected]>
@picnixz picnixz added the stdlib Python modules in the Lib dir label Sep 11, 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 <[email protected]>
@hauntsaninja
Copy link
Contributor

To avoid compatibility issues, it would be good to fix this in 3.13.0 so I added release-blocker. Please let me know if that's inappropriate

@hauntsaninja hauntsaninja added the 3.13 bugs and security fixes label Sep 12, 2024
@Eclips4 Eclips4 added the 3.14 new features, bugs and security fixes label Sep 12, 2024
miss-islington pushed a commit to miss-islington/cpython that referenced this issue Sep 12, 2024
Yhg1s pushed a commit that referenced this issue Sep 23, 2024
…123971) (#124009)

gh-123968: Fix lower bound for `python -m random --float` (GH-123971)
(cherry picked from commit a362c41)

Co-authored-by: Anders Kaseorg <[email protected]>
@hugovk
Copy link
Member

hugovk commented Sep 23, 2024

Merged and backported, thanks!

@hugovk hugovk closed this as completed Sep 23, 2024
nanjekyejoannah pushed a commit that referenced this issue Sep 25, 2024
* fix -f/--float command line option description

See gh-123968 gh-124009

* Update Doc/library/random.rst

---------

Co-authored-by: Sergey B Kirpichev <[email protected]>
miss-islington pushed a commit to miss-islington/cpython that referenced this issue Sep 25, 2024
…onGH-124517)

* fix -f/--float command line option description

See pythongh-123968 pythongh-124009

* Update Doc/library/random.rst

---------

(cherry picked from commit f8651a2)

Co-authored-by: Robert Wolff <[email protected]>
Co-authored-by: Sergey B Kirpichev <[email protected]>
Yhg1s pushed a commit that referenced this issue Sep 26, 2024
…124517) (#124523)

gh-123968: fix -f/--float command line option description (GH-124517)

* fix -f/--float command line option description

See gh-123968 gh-124009

* Update Doc/library/random.rst

---------

(cherry picked from commit f8651a2)

Co-authored-by: Robert Wolff <[email protected]>
Co-authored-by: Sergey B Kirpichev <[email protected]>
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 3.14 new features, bugs and security fixes release-blocker stdlib Python modules in the Lib dir type-bug An unexpected behavior, bug, or error
Projects
Development

No branches or pull requests

5 participants