Zsh Mailing List Archive
Messages sorted by: Reverse Date, Date, Thread, Author

Re: Bug report: Strange behaviour with random, sort and uniq



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




Messages sorted by: Reverse Date, Date, Thread, Author