Skip to content

Commit 7b8affa

Browse files
[3.13] gh-123968: Fix lower bound for python -m random --float (GH-123971) (#124009)
gh-123968: Fix lower bound for `python -m random --float` (GH-123971) (cherry picked from commit a362c41) Co-authored-by: Anders Kaseorg <andersk@mit.edu>
1 parent a439f85 commit 7b8affa

File tree

3 files changed

+6
-5
lines changed

3 files changed

+6
-5
lines changed

Lib/random.py

+3-3
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)
10421042

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())

Lib/test/test_random.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -1433,8 +1433,8 @@ def test_main(self):
14331433
("'a a' 'b b' 'c c'", "b b"),
14341434
("--integer 5", 4),
14351435
("5", 4),
1436-
("--float 2.5", 2.266632777287572),
1437-
("2.5", 2.266632777287572),
1436+
("--float 2.5", 2.1110546288126204),
1437+
("2.5", 2.1110546288126204),
14381438
]:
14391439
random.seed(0)
14401440
self.assertEqual(random.main(shlex.split(command)), expected)
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)