zsh-users
 help / color / mirror / code / Atom feed
From: Peter Stephenson <p.w.stephenson@ntlworld.com>
To: zsh-users@zsh.org
Subject: Re: random once but not twice
Date: Sun, 12 Nov 2017 19:08:06 +0000	[thread overview]
Message-ID: <20171112190806.7f2cf68a@ntlworld.com> (raw)
In-Reply-To: <86375k5t8w.fsf@zoho.com>

On Sat, 11 Nov 2017 23:46:07 +0100
Emanuel Berg <moasen@zoho.com> 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


  reply	other threads:[~2017-11-12 19:08 UTC|newest]

Thread overview: 12+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2017-11-11 18:57 Emanuel Berg
2017-11-11 21:52 ` Mikael Magnusson
2017-11-11 22:26   ` Emanuel Berg
2017-11-11 22:32     ` Emanuel Berg
2017-11-11 22:46       ` Emanuel Berg
2017-11-12 19:08         ` Peter Stephenson [this message]
2017-11-12 20:02           ` Emanuel Berg
2017-11-12 20:15             ` Bart Schaefer
2017-11-12 20:18               ` Emanuel Berg
2017-11-13  0:15           ` Emanuel Berg
2017-11-13  4:56             ` Bart Schaefer
2017-11-13  5:07               ` Emanuel Berg

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=20171112190806.7f2cf68a@ntlworld.com \
    --to=p.w.stephenson@ntlworld.com \
    --cc=zsh-users@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).