Zsh Mailing List Archive
Messages sorted by:
Reverse Date,
Date,
Thread,
Author
Re: Bug/regression: tt(RANDOM) seed not updating when $RANDOM is used in a pipe
On Sun, Jul 2, 2023 at 9:19 AM Mikael Magnusson <mikachu@xxxxxxxxx> wrote:
>
> On 6/28/23, Jon Oster <jon.i.oster@xxxxxxxxx> wrote:
> > In 5.8.1, $RANDOM seems not to update its state when the command $RANDOM
> > was used in is piped somewhere. For example:
> >
> > # echo $RANDOM
> > 18163
> > # echo $RANDOM
> > 23472
> > # echo $RANDOM
> > 27685
> > # echo $RANDOM|cat
> > 10178
> > # echo $RANDOM|cat
> > 10178
> > # echo $RANDOM
> > 10178
> > # echo $RANDOM
> > 17862
>
> A simple workaround for this, whether you consider it a bug or a feature,
> % () { echo $1 | cat } $RANDOM
> 21589
> % () { echo $1 | cat } $RANDOM
> 301
>
> or
> % echo $RANDOM > >(cat)
> 1324
> % echo $RANDOM > >(cat)
> 9460
An alternative workaround is to use a random number generator whose
state is kept outside of the current process. Here's an example:
https://gist.github.com/romkatv/a6cede40714ec77d4da73605c5ddb36a. An
added advantage of this approach is that you get a 32-bit number
rather than the 15-bit provided by $RANDOM.
Roman.
Messages sorted by:
Reverse Date,
Date,
Thread,
Author