zsh-workers
 help / color / mirror / code / Atom feed
* Bug report: Strange behaviour with random, sort and uniq
@ 2023-02-25 23:02 Sheogorath
  2023-02-25 23:21 ` Lawrence Velázquez
  0 siblings, 1 reply; 2+ messages in thread
From: Sheogorath @ 2023-02-25 23:02 UTC (permalink / raw)
  To: zsh-workers

Hello ZSH maintainers,

I found a quite strange bug in my current installation. I tried to do
some virtual dice flips and something strange appeared.

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:

Example:
$ for i in $(seq 0 100); do echo $((1 + RANDOM % 6)); done | sort |
uniq -c
     12 1
     17 2
     22 3
     19 4
     16 5
     15 6

$ for i in $(seq 0 100); do echo $((1 + RANDOM % 6)); done | sort |
uniq -c
     12 1
     17 2
     22 3
     19 4
     16 5
     15 6


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

Next time, the output of 'for i in $(seq 0 100); do echo $((1 + RANDOM
% 6)); done | sort | uniq -c' will be different.

When redirecting the output of the command into files, these are also
different.

Since all of this was so strange, I reproduced it on Fedora 36 in a
container (using zsh -f) as well as on my Fedora 37 host. Also in a VM,
with a fresh Fedora 37 install on Hetzner and a Debian 10 install, to
make sure, it's not due to a distro specific modification.

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

I hope you'll have fun debugging it, I have no idea where to even
start.

Greetings
Sheogorath


^ permalink raw reply	[flat|nested] 2+ messages in thread

* Re: Bug report: Strange behaviour with random, sort and uniq
  2023-02-25 23:02 Bug report: Strange behaviour with random, sort and uniq Sheogorath
@ 2023-02-25 23:21 ` Lawrence Velázquez
  0 siblings, 0 replies; 2+ messages in thread
From: Lawrence Velázquez @ 2023-02-25 23:21 UTC (permalink / raw)
  To: Sheogorath; +Cc: zsh-workers

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


^ permalink raw reply	[flat|nested] 2+ messages in thread

end of thread, other threads:[~2023-02-25 23:22 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2023-02-25 23:02 Bug report: Strange behaviour with random, sort and uniq Sheogorath
2023-02-25 23:21 ` Lawrence Velázquez

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).