From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: (qmail 23267 invoked by alias); 24 Feb 2015 02:08:48 -0000 Mailing-List: contact zsh-workers-help@zsh.org; run by ezmlm Precedence: bulk X-No-Archive: yes List-Id: Zsh Workers List List-Post: List-Help: X-Seq: 34629 Received: (qmail 12837 invoked from network); 24 Feb 2015 02:08:40 -0000 X-Spam-Checker-Version: SpamAssassin 3.3.2 (2011-06-06) on f.primenet.com.au X-Spam-Level: X-Spam-Status: No, score=-2.7 required=5.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,FREEMAIL_FROM,RCVD_IN_DNSWL_LOW autolearn=ham version=3.3.2 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=mime-version:in-reply-to:references:date:message-id:subject:from:to :cc:content-type; bh=QQ83Ctu4c1J4LIjFAgyMmYezz0Ro3EQrlxXKCwp72vk=; b=m/Mt57pV8JNgi0tqkcTLQS1ZQjwqMNY/sgWpdeymnBTQRN6vvsriGd9PbbNhJ4PTi8 Jux42CoFDsmnez7YHt2qvuFBrbrfqkDF4XgRf5oKEe2olAwoC4xrr9yWJBAqDWB5xolS OH7AtWGsB1Y+0L0oMqnKU1mxU9ERAluwUdzBG785CDLaiWYBuyBRIE/E4ogiH11sAPiA uma/TKqjugEwjuu+8hL2OG9HYTrVscL+P8bC6PF7NncZtd4VuNTXmvRsUjICkYORVfI5 +qENODZOoKB20t/6D64Y/V+6NMtxj4tEJthK9HDGu5p/779XT5TdlFNaVJShjuUVK0vn LyNg== MIME-Version: 1.0 X-Received: by 10.42.138.199 with SMTP id d7mr14753227icu.3.1424743714325; Mon, 23 Feb 2015 18:08:34 -0800 (PST) In-Reply-To: References: <5D99F52C-D21D-43C3-B76E-99223847D79B@iki.fi> Date: Tue, 24 Feb 2015 03:08:34 +0100 Message-ID: Subject: Re: $RANDOM initial state doesn't change From: Mikael Magnusson To: Jan Larres Cc: zsh workers Content-Type: text/plain; charset=UTF-8 On Tue, Feb 24, 2015 at 12:27 AM, Jan Larres 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 > 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