zsh-workers
 help / color / mirror / code / Atom feed
From: "Lawrence Velázquez" <larryv@zsh.org>
To: Sheogorath <zsh@sheogorath.shivering-isles.com>
Cc: zsh-workers@zsh.org
Subject: Re: Bug report: Strange behaviour with random, sort and uniq
Date: Sat, 25 Feb 2023 18:21:56 -0500	[thread overview]
Message-ID: <97de94e6-ec50-4f38-962a-c066593bccbf@app.fastmail.com> (raw)
In-Reply-To: <010701868ad0a7e8-be8c1ddd-a63c-4002-926a-b7ba25a0cebb-000000@eu-central-1.amazonses.com>

On Sat, Feb 25, 2023, at 6:02 PM, Sheogorath wrote:
> I was running the following command in a terminal:
>
> for i in $(seq 0 100); do echo $((1 + RANDOM % 6)); done | sort | uniq
> -c
>
> When run twice after each other, it produces the same output:

This command uses RANDOM in a subshell.

https://zsh.sourceforge.io/Doc/Release/Parameters.html#index-RANDOM

	The values of RANDOM form an intentionally-repeatable
	pseudo-random sequence; subshells that reference RANDOM
	will result in identical pseudo-random values unless the
	value of RANDOM is referenced or seeded in the parent shell
	in between subshell invocations.


> However, this changes, as soon as one run the command without the 'sort
> | uniq -c' piping:
>
> for i in $(seq 0 100); do echo $((1 + RANDOM % 6)); done

This command does not use RANDOM in a subshell.


> I also validated, that this behaviour doesn't exist in bash.

RANDOM works differently in bash.


-- 
vq


      reply	other threads:[~2023-02-25 23:22 UTC|newest]

Thread overview: 2+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2023-02-25 23:02 Sheogorath
2023-02-25 23:21 ` Lawrence Velázquez [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=97de94e6-ec50-4f38-962a-c066593bccbf@app.fastmail.com \
    --to=larryv@zsh.org \
    --cc=zsh-workers@zsh.org \
    --cc=zsh@sheogorath.shivering-isles.com \
    /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).