8000 gh-123968: Fix lower bound for `python -m random --float` · python/cpython@98fb826 · GitHub
[go: up one dir, main page]

Skip to content

Commit 98fb826

Browse files
committed
gh-123968: Fix lower bound for python -m random --float
`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 #118131, and as everyone will expect. Fixes #123968. Signed-off-by: Anders Kaseorg <andersk@mit.edu>
1 parent 3bd942f commit 98fb826

File tree

2 files changed

+4
-3
lines changed

2 files changed

+4
-3
lines changed

Lib/random.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1013,7 +1013,7 @@ def _parse_args(arg_list: list[str] | None):
10131013
help="print a random integer between 1 and N inclusive")
10141014
group.add_argument(
10151015
"-f", "--float", type=float, metavar="N",
1016-
help="print a random floating-point number between 1 and N inclusive")
1016+
help="print a random floating-point number between 0 and N inclusive")
10171017
group.add_argument(
10181018
"--test", type=int, const=10_000, nargs="?",
10191019
help=argparse.SUPPRESS)
@@ -1038,7 +1038,7 @@ def main(arg_list: list[str] | None = None) -> int | str:
10381038
return randint(1, args.integer)
10391039

10401040
if args.float is not None:
1041-
return uniform(1, args.float)
1041+
return uniform(0, args.float)
1042< 8000 /td>1042

10431043
if args.test:
10441044
_test(args.test)
@@ -1055,7 +1055,7 @@ def main(arg_list: list[str] | None = None) -> int | str:
10551055
try:
10561056
# Is it a float?
10571057
val = float(val)
1058-
return uniform(1, val)
1058+
return uniform(0, val)
10591059
except ValueError:
10601060
# Split in case of space-separated string: "a b c"
10611061
return choice(val.split())
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Fix the command-line interface for the :mod:`random` module to select floats between 0 and N, not 1 and N.

0 commit comments

Comments
 (0)
0