mirror of
https://github.com/fish-shell/fish-shell.git
synced 2026-06-19 21:21:15 -03:00
builtin random: Be less strict about arguments
This now allows: - Same argument (`random 5 5`) - Swapped ends (`random 10 2`) - One possibility (`random 0 5 4`) This makes it easier to use with numbers generated elsewhere instead of hard-coded, so you don't need to check as much before running it. Fixes #10879
This commit is contained in:
@@ -135,22 +135,14 @@ fn parse_ull(streams: &mut IoStreams, cmd: &wstr, num: &wstr) -> Result<u64, wut
|
||||
}
|
||||
}
|
||||
|
||||
if end <= start {
|
||||
streams
|
||||
.err
|
||||
.append(wgettext_fmt!("%ls: END must be greater than START\n", cmd,));
|
||||
return STATUS_INVALID_ARGS;
|
||||
}
|
||||
let (start, end) = if start <= end {
|
||||
(start, end)
|
||||
} else {
|
||||
(end, start)
|
||||
};
|
||||
|
||||
// Using abs_diff() avoids an i64 overflow if start is i64::MIN and end is i64::MAX
|
||||
let possibilities = end.abs_diff(start) / step;
|
||||
if possibilities == 0 {
|
||||
streams.err.append(wgettext_fmt!(
|
||||
"%ls: range contains only one possible value\n",
|
||||
cmd,
|
||||
));
|
||||
return STATUS_INVALID_ARGS;
|
||||
}
|
||||
|
||||
let rand = {
|
||||
let mut engine = RNG.lock().unwrap();
|
||||
|
||||
Reference in New Issue
Block a user