Skip to content

gh-123968: Fix lower bound for python -m random --float #123971

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

Merged
merged 1 commit into from
Sep 12, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions Lib/random.py
Original file line number Diff line number Diff line change
Expand Up @@ -1013,7 +1013,7 @@ def _parse_args(arg_list: list[str] | None):
help="print a random integer between 1 and N inclusive")
group.add_argument(
"-f", "--float", type=float, metavar="N",
help="print a random floating-point number between 1 and N inclusive")
help="print a random floating-point number between 0 and N inclusive")
group.add_argument(
"--test", type=int, const=10_000, nargs="?",
help=argparse.SUPPRESS)
Expand All @@ -1038,7 +1038,7 @@ def main(arg_list: list[str] | None = None) -> int | str:
return randint(1, args.integer)

if args.float is not None:
return uniform(1, args.float)
return uniform(0, args.float)

if args.test:
_test(args.test)
Expand All @@ -1055,7 +1055,7 @@ def main(arg_list: list[str] | None = None) -> int | str:
try:
# Is it a float?
val = float(val)
return uniform(1, val)
return uniform(0, val)
except ValueError:
# Split in case of space-separated string: "a b c"
return choice(val.split())
Expand Down
4 changes: 2 additions & 2 deletions Lib/test/test_random.py
Original file line number Diff line number Diff line change
Expand Up @@ -1433,8 +1433,8 @@ def test_main(self):
("'a a' 'b b' 'c c'", "b b"),
("--integer 5", 4),
("5", 4),
("--float 2.5", 2.266632777287572),
("2.5", 2.266632777287572),
("--float 2.5", 2.1110546288126204),
("2.5", 2.1110546288126204),
]:
random.seed(0)
self.assertEqual(random.main(shlex.split(command)), expected)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Fix the command-line interface for the :mod:`random` module to select floats between 0 and N, not 1 and N.
Loading