From: Mikael Magnusson <mikachu@gmail.com>
To: Jan Larres <jan@majutsushi.net>
Cc: zsh workers <zsh-workers@zsh.org>
Subject: Re: $RANDOM initial state doesn't change
Date: Tue, 24 Feb 2015 03:08:34 +0100 [thread overview]
Message-ID: <CAHYJk3TCRWZ1yQxF+nW9FyrMdd5DGexjSyY=joiDS+FagAOhwg@mail.gmail.com> (raw)
In-Reply-To: <mcgd0s$rhc$1@ger.gmane.org>
On Tue, Feb 24, 2015 at 12:27 AM, Jan Larres <jan@majutsushi.net> wrote:
> On 24/02/15 09:22, Timo Sirainen wrote:
>> I was trying to use $RANDOM for a simple 1/0 check, but it kept failing.
>> After a while I realized a new subshell always gives the same $RANDOM
>> result:
>>
>> % for i in {1..10}; do echo `echo $RANDOM`; sleep 1; done
>> 13490
>> 13490
>> 13490
>> 13490
>> 13490
>> 13490
>> 13490
>> 13490
>> 13490
>> 13490
>>
>> Surely it should be more random than that?
>
> From the manual:
>
> RANDOM <S>
> A pseudo-random integer from 0 to 32767, newly generated each
> time this parameter is referenced. The random number generator
> can be seeded by assigning a numeric value to 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.
>
>
> $ for i in {1..10}; do echo $(echo $RANDOM) $RANDOM; done
> 30686 30686
> 8933 8933
> 4452 4452
> 6983 6983
> 21425 21425
> 27288 27288
> 18721 18721
> 22501 22501
> 1008 1008
> 29465 29465
You can use anonymous functions to always evaluate $RANDOM in the
parent shell (unless of course the whole loop is subshelled),
% for i in {1..10}; do () { echo $(echo $1) $2 } $RANDOM $RANDOM; done
--
Mikael Magnusson
prev parent reply other threads:[~2015-02-24 2:08 UTC|newest]
Thread overview: 3+ messages / expand[flat|nested] mbox.gz Atom feed top
2015-02-23 20:22 Timo Sirainen
2015-02-23 23:27 ` Jan Larres
2015-02-24 2:08 ` Mikael Magnusson [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='CAHYJk3TCRWZ1yQxF+nW9FyrMdd5DGexjSyY=joiDS+FagAOhwg@mail.gmail.com' \
--to=mikachu@gmail.com \
--cc=jan@majutsushi.net \
--cc=zsh-workers@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).