summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorAnders Kaseorg <andersk@mit.edu>2024-09-12 13:54:18 (GMT)
committerGitHub <noreply@github.com>2024-09-12 13:54:18 (GMT)
commita362c41bc934fabe6bfef9be1962005b38396860 (patch)
tree7b7acb6a792c60e00d5bd0e5f873bf77fc783281
parent8e99495701737c9d9706622f59581213ef163b23 (diff)
downloadcpython-a362c41bc934fabe6bfef9be1962005b38396860.zip
cpython-a362c41bc934fabe6bfef9be1962005b38396860.tar.gz
cpython-a362c41bc934fabe6bfef9be1962005b38396860.tar.bz2
gh-123968: Fix lower bound for `python -m random --float` (#123971)
-rw-r--r--Lib/random.py6
-rw-r--r--Lib/test/test_random.py4
-rw-r--r--Misc/NEWS.d/next/Library/2024-09-11-19-12-23.gh-issue-123968.OwHON_.rst1
3 files changed, 6 insertions, 5 deletions
diff --git a/Lib/random.py b/Lib/random.py
index f5a482b..8b9a270 100644
--- a/Lib/random.py
+++ b/Lib/random.py
@@ -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)
@@ -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)
@@ -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())
diff --git a/Lib/test/test_random.py b/Lib/test/test_random.py
index 9a44ab1..51f9193 100644
--- a/Lib/test/test_random.py
+++ b/Lib/test/test_random.py
@@ -1433,8 +1433,8 @@ class CommandLineTest(unittest.TestCase):
("'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)
diff --git a/Misc/NEWS.d/next/Library/2024-09-11-19-12-23.gh-issue-123968.OwHON_.rst b/Misc/NEWS.d/next/Library/2024-09-11-19-12-23.gh-issue-123968.OwHON_.rst
new file mode 100644
index 0000000..4d48947
--- /dev/null
+++ b/Misc/NEWS.d/next/Library/2024-09-11-19-12-23.gh-issue-123968.OwHON_.rst
@@ -0,0 +1 @@
+Fix the command-line interface for the :mod:`random` module to select floats between 0 and N, not 1 and N.