zsh-workers
 help / color / mirror / code / Atom feed
* $RANDOM initial state doesn't change
@ 2015-02-23 20:22 Timo Sirainen
  2015-02-23 23:27 ` Jan Larres
  0 siblings, 1 reply; 3+ messages in thread
From: Timo Sirainen @ 2015-02-23 20:22 UTC (permalink / raw)
  To: zsh-workers

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?

Tested with:

zsh 5.0.7 (x86_64-pc-linux-gnu)
zsh 5.0.2 (x86_64-apple-darwin12.2.0)


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

* Re: $RANDOM initial state doesn't change
  2015-02-23 20:22 $RANDOM initial state doesn't change Timo Sirainen
@ 2015-02-23 23:27 ` Jan Larres
  2015-02-24  2:08   ` Mikael Magnusson
  0 siblings, 1 reply; 3+ messages in thread
From: Jan Larres @ 2015-02-23 23:27 UTC (permalink / raw)
  To: zsh-workers

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

-Jan


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

* Re: $RANDOM initial state doesn't change
  2015-02-23 23:27 ` Jan Larres
@ 2015-02-24  2:08   ` Mikael Magnusson
  0 siblings, 0 replies; 3+ messages in thread
From: Mikael Magnusson @ 2015-02-24  2:08 UTC (permalink / raw)
  To: Jan Larres; +Cc: zsh workers

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


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

end of thread, other threads:[~2015-02-24  2:08 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2015-02-23 20:22 $RANDOM initial state doesn't change Timo Sirainen
2015-02-23 23:27 ` Jan Larres
2015-02-24  2:08   ` Mikael Magnusson

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