Zsh Mailing List Archive
Messages sorted by:
Reverse Date,
Date,
Thread,
Author
Re: [PATCH] zsh/random module [UPDATED]
Clinton Bunch wrote on Wed, Nov 23, 2022 at 07:41:40 -0600:
> I don't think we're going to come to an agreement on inclusive vs exclusive,
> and I know you'd hate the idea of both interfaces, so others will have to
> weigh in.
Some considerations:
1. What existing API, within or without zsh, should the new
builtin/mathfunc be consistent with?
2. Is it the case that choice A can be implemented in shell code in
terms of choice B, but not vice versa? [For instance, if the argument
to -U were a uint64_t and were exclusive, there'd be no way to specify
UINT64_MAX as the upper bound.]
3. Inclusive is necessarily the rule when sampling from a given set of
elements (e.g., in zsh terms, "here's an array name, gimme a random
element from it"; when the array contains, say, {January..December}).
In this case there's no need to involve integers at all.
4. Which approach generalizes better?
---
[Please consider these questions for a second before reading on; my
proposed answer(s) are below.]
---
For #1, we might want to be consistent with the subscript slice syntax.
$foo[4,5] selects $foo[4] and $foo[5] both. Similarly {4..5} includes
both endpoints.
Also, I could be wrong here, but isn't it the case that exclusiveness
goes along with the lower bound being hard-coded as zero? That's how it
is in modular arithmetic, in arc4random_uniform(), and in Perl, and even
in C's idiom for iterating a dynamically-allocated array («malloc(42 * …)»,
followed by «for (i = 0; i < 42; …)»). Python has both random.randint(),
which is inclusive, and random.randrange(), which is exclusive — but the
latter mirrors Python's array slice syntax, and that syntax has
a special-case form for "All elements from the Nth through the last one,
inclusive".
I think I'm leaning towards inclusive if -L and -U can both be specified
and towards exclusive if -L is hard-coded as 0, but I'm happy to be
convinced otherwise.
HTH,
Daniel
Messages sorted by:
Reverse Date,
Date,
Thread,
Author