zsh-workers
 help / color / Atom feed
* broken random variable
@ 2019-10-19  5:38 . .
  2019-10-19  5:44 ` Roman Perepelitsa
                   ` (2 more replies)
  0 siblings, 3 replies; 5+ messages in thread
From: . . @ 2019-10-19  5:38 UTC (permalink / raw)
  To: zsh-workers

Hello, zsh-workers

I found a bug in the $RANDOM variable; on OpenBSD, the command "echo $RANDOM | tee" will print the same number
across different executions of the same command, and the value of "echo $RANDOM" will be the same as the last value.
At first I thought that this bug was limited to OpenBSD, because it was not reproduced on the website "rootnroll" which has
a demo for "oh my zsh", but today I tested another command on the website:  "yes | while read; do echo $RANDOM; done | head",
and all the random numbers are the same across different executions of the command. I hope that's enough information for you.
I reported the first bug to OpenBSD on monday, and Theo de Raadt replied and said that I should tell you instead.
I guess this  e-mail might be a bit late, but I was waiting for evidence that it can be reproduced elsewhere.

PS: I would recommend using "arc4random" on OpenBSD if that is at all possible.

Thanks for reading my message. Good day.


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

* Re: broken random variable
  2019-10-19  5:38 broken random variable . .
@ 2019-10-19  5:44 ` Roman Perepelitsa
  2019-10-19  6:09   ` Daniel Shahaf
  2019-10-19  9:48 ` Sebastian Gniazdowski
  2019-10-19 14:31 ` Mikael Magnusson
  2 siblings, 1 reply; 5+ messages in thread
From: Roman Perepelitsa @ 2019-10-19  5:44 UTC (permalink / raw)
  To: . .; +Cc: Zsh hackers list

Simpler test case:

    repeat 2 ( echo $RANDOM )

Prints the same number twice. Tested on FreeBSD and Linux.

Roman.

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

* Re: broken random variable
  2019-10-19  5:44 ` Roman Perepelitsa
@ 2019-10-19  6:09   ` Daniel Shahaf
  0 siblings, 0 replies; 5+ messages in thread
From: Daniel Shahaf @ 2019-10-19  6:09 UTC (permalink / raw)
  To: Roman Perepelitsa, . .; +Cc: Zsh hackers list

Roman Perepelitsa wrote on Sat, 19 Oct 2019 05:44 +00:00:
> Simpler test case:
> 
>     repeat 2 ( echo $RANDOM )
> 
> Prints the same number twice. Tested on FreeBSD and Linux.

Yes, it's documented to behave this way.

man zshall | less -p RANDOM

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

* Re: broken random variable
  2019-10-19  5:38 broken random variable . .
  2019-10-19  5:44 ` Roman Perepelitsa
@ 2019-10-19  9:48 ` Sebastian Gniazdowski
  2019-10-19 14:31 ` Mikael Magnusson
  2 siblings, 0 replies; 5+ messages in thread
From: Sebastian Gniazdowski @ 2019-10-19  9:48 UTC (permalink / raw)
  To: . .; +Cc: Zsh hackers list

On Sat, 19 Oct 2019 at 07:39, . . <apatiskogen@yandex.com> wrote:
>
> Hello, zsh-workers
>
> I found a bug in the $RANDOM variable; on OpenBSD, the command "echo $RANDOM | tee" will print the same number
> across different executions of the same command, and the value of "echo $RANDOM" will be the same as the last value.

That's because Zsh is different from other shells in one regard: for the pipe:

A | B

it runs A in a subshell and B in the current shell, while other shells
like Bash run A in the current shell and B in the subshell. This is a
very nice feature, as it allows to do:

cat /some/file | while read line; do some_var=…; done
print $some_var

and the side-effects of this pipe will survive to the `print'.

So, the `echo $RANDOM | tee` will print the same value because the
$RANDOM will be read in a subshell, meaning that a fork() will be done
and the random-seed will be left unchanged in the outer process.

-- 
Sebastian Gniazdowski
News: https://twitter.com/ZdharmaI
IRC: https://kiwiirc.com/client/chat.freenode.net:+6697/#zplugin
Blog: http://zdharma.org

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

* Re: broken random variable
  2019-10-19  5:38 broken random variable . .
  2019-10-19  5:44 ` Roman Perepelitsa
  2019-10-19  9:48 ` Sebastian Gniazdowski
@ 2019-10-19 14:31 ` Mikael Magnusson
  2 siblings, 0 replies; 5+ messages in thread
From: Mikael Magnusson @ 2019-10-19 14:31 UTC (permalink / raw)
  To: . .; +Cc: zsh-workers

On 10/19/19, . . <apatiskogen@yandex.com> wrote:
> Hello, zsh-workers
>
> PS: I would recommend using "arc4random" on OpenBSD if that is at all
> possible.

OpenBSD actually incorrectly does this by default for rand() and we
had to go to some lengths to disable that misfeature. If you use old
versions of zsh[1] on new versions of OpenBSD, it will incorrectly
behave as you expect.


[1] http://www.zsh.org/mla/workers/2016/msg01503.html

-- 
Mikael Magnusson

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

end of thread, back to index

Thread overview: 5+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2019-10-19  5:38 broken random variable . .
2019-10-19  5:44 ` Roman Perepelitsa
2019-10-19  6:09   ` Daniel Shahaf
2019-10-19  9:48 ` Sebastian Gniazdowski
2019-10-19 14:31 ` Mikael Magnusson

zsh-workers

Archives are clonable: git clone --mirror http://inbox.vuxu.org/zsh-workers

Example config snippet for mirrors

Newsgroup available over NNTP:
	nntp://inbox.vuxu.org/vuxu.archive.zsh.workers


AGPL code for this site: git clone https://public-inbox.org/public-inbox.git