Zsh Mailing List Archive
Messages sorted by:
Reverse Date,
Date,
Thread,
Author
Re: random once but not twice
- X-seq: zsh-users 22968
- From: Peter Stephenson <p.w.stephenson@xxxxxxxxxxxx>
- To: zsh-users@xxxxxxx
- Subject: Re: random once but not twice
- Date: Sun, 12 Nov 2017 19:08:06 +0000
- Dkim-signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=ntlworld.com; s=meg.feb2017; t=1510513687; bh=S73kM4GrF9vrgcosNyBwg6meuvCeRxRADpKbg8AHhos=; h=Date:From:To:Subject:In-Reply-To:References; b=aeblw9zLSEQUQv5ZmsUiH7zNIRenXnTrrbySM6oVk1AK/oG8FGODTdgNJF5f/dC4G ISAvBRlVFLfIVXHDjPyP0WG4LzKdPopQP3H8Yg10B5hFLVqfbFERXFXJMxkvTHtpNt TUnMco/DfufukxQEJXe65Mhe+tPBhbEJXEadkJy19GgE89GuNgPaeSlf/hz7T8PF6P H/NoKlkCMUdJRRhCcitliet+RsbXb1tQ8n9fjkGWsRyxpxY7auaDLq3HgKu6Jpf3Xb rUKTwF8ddMCUH/4IF1txiwb/8fAHqr8kwF6Q/2JCG5EnXatIKbc3LRXL8WwM+UNyn0 TzRrV/+FBfKCQ==
- In-reply-to: <86375k5t8w.fsf@zoho.com>
- List-help: <mailto:zsh-users-help@zsh.org>
- List-id: Zsh Users List <zsh-users.zsh.org>
- List-post: <mailto:zsh-users@zsh.org>
- List-unsubscribe: <mailto:zsh-users-unsubscribe@zsh.org>
- Mailing-list: contact zsh-users-help@xxxxxxx; run by ezmlm
- References: <86k1yw63u0.fsf@zoho.com> <CAHYJk3SE_JV0BHY55vjgqSeDeM5vTOB=d1UH_Ok-yn0WBZv4Zg@mail.gmail.com> <86bmk85u6b.fsf@zoho.com> <867euw5twc.fsf@zoho.com> <86375k5t8w.fsf@zoho.com>
On Sat, 11 Nov 2017 23:46:07 +0100
Emanuel Berg <moasen@xxxxxxxx> wrote:
> Emanuel Berg wrote:
>
> > echo $(echo $RANDOM) # always the same
>
> OK, I learned on #zsh@freenode that $RANDOM
> doesn't do anything but is inherited from the
> parent shell where there has been no access and
> thus no new value.
>
> : $RANDOM; vid=$(random-video $vid_dir)
You can also work around with the following rand48 function.
(rand48; print $REPLY)
(rand48; print $REPLY)
pws
# Genrate a 48-bit pseudo-random number as a floating point value between
# 0 and 1 in $REPLY.
#
# This version is slow but works aroud the fact that the random number
# seed doesn't propagate back from a subshell by storing the seed in
# a file.
#
# You can speicify a file name, else ${ZDOTDIR:-~}/.zsh-rand48-seeed is used,
# but if using the default be careful about simultaneous accesses from
# multipe shells.
zmodload zsh/mathfunc
local file=${1:-${ZDOTDIR:-~}/.zsh-rand48-seed}
local seed
if [[ -f $file ]]; then
seed=$(<$file)
fi
typeset -g REPLY=$(( rand48(seed) ))
print $seed >$file
Messages sorted by:
Reverse Date,
Date,
Thread,
Author