zsh-users
 help / color / mirror / code / Atom feed
From: Roman Perepelitsa <roman.perepelitsa@gmail.com>
To: Mikael Magnusson <mikachu@gmail.com>
Cc: Jon Oster <jon.i.oster@gmail.com>, zsh-users@zsh.org
Subject: Re: Bug/regression: tt(RANDOM) seed not updating when $RANDOM is used in a pipe
Date: Sun, 2 Jul 2023 09:29:29 +0200	[thread overview]
Message-ID: <CAN=4vMoOikPvfbzqM3weAsNWnNPTjHW_49q3RuW4Xv_gAgv=zg@mail.gmail.com> (raw)
In-Reply-To: <CAHYJk3ThphEYqo04Y3YjT4T1KgzmrvbQ+xPwTK1iJcjGCcyGfw@mail.gmail.com>

On Sun, Jul 2, 2023 at 9:19 AM Mikael Magnusson <mikachu@gmail.com> wrote:
>
> On 6/28/23, Jon Oster <jon.i.oster@gmail.com> 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.


      reply	other threads:[~2023-07-02  7:30 UTC|newest]

Thread overview: 10+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2023-06-28 20:10 Jon Oster
2023-06-29  2:46 ` Eric Cook
2023-06-29  5:42   ` Bart Schaefer
2023-06-29 14:41     ` Ray Andrews
2023-06-29 14:56     ` Roman Perepelitsa
2023-06-29 17:53       ` Mikael Magnusson
2023-06-29 18:15         ` Roman Perepelitsa
2023-06-29 23:41           ` Bart Schaefer
2023-07-02  7:17 ` Mikael Magnusson
2023-07-02  7:29   ` Roman Perepelitsa [this message]

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to='CAN=4vMoOikPvfbzqM3weAsNWnNPTjHW_49q3RuW4Xv_gAgv=zg@mail.gmail.com' \
    --to=roman.perepelitsa@gmail.com \
    --cc=jon.i.oster@gmail.com \
    --cc=mikachu@gmail.com \
    --cc=zsh-users@zsh.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
Code repositories for project(s) associated with this public inbox

	https://git.vuxu.org/mirror/zsh/

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).