From: Phil Pennock <zsh-workers+phil.pennock@spodhuis.org>
To: Mikael Magnusson <mikachu@gmail.com>
Cc: zsh workers <zsh-workers@zsh.org>
Subject: Re: funny subshell effect
Date: Wed, 9 Mar 2011 19:29:31 -0500 [thread overview]
Message-ID: <20110310002931.GA40551@redoubt.spodhuis.org> (raw)
In-Reply-To: <AANLkTi=VUkvqOxo+OANG2AVYko-Zbq7LwLQoF68fbLev@mail.gmail.com>
On 2011-03-10 at 00:18 +0100, Mikael Magnusson wrote:
> This confused me for a few minutes, I was trying to play a random midi file with
> pmidi *(oe:REPLY=\$RANDOM:[1])
> but it plays the same one each time, but when i tried
> echo *(oe:REPLY=\$RANDOM:[1])
> it printed a different one each time
>
> % repeat 3; do echo .(e:REPLY=\$RANDOM:); done
> 17
> 25549
> 6369
> % repeat 3; do command echo .(e:REPLY=\$RANDOM:); done
> 5801
> 5801
> 5801
>
> Is this something that must be so? (My guess is yes, but it can't hurt to ask).
> (I know I can work around it easily by assigning to a var first).
If you assign to RANDOM in the subshell, that's a call to srand() which
will re-seed the RNG.
At present, the retrieval is just:
zlong
randomgetfn(UNUSED(Param pm))
{
return rand() & 0x7fff;
}
Given the amount of forking going on normally, reseeding on every fork
would be bad, but perhaps something similar to the code I added to Exim
would work, so:
zlong
randomgetfn(UNUSED(Param pm))
{
static pid_t last_rand_pid = 0;
pid_t p;
p = getpid()
if (p != last_rand_pid)
{
last_rand_pid = p;
#ifdef HAVE_SRANDOMDEV_OR_WHATEVER_ZSH_HAS_AS_GUARD
srandomdev();
#else
gettimeofday(&tv, NULL);
srandom(tv.tv_sec | tv.tv_usec | getpid());
#endif
}
return rand() & 0x7fff;
}
Except that I actually made Exim prefer arc4random()/arc4random_stir()
if available, and actually uses much stronger randomness from OpenSSL if
linked against that, but that's because people cook up homebrew access
token stuff and I wanted to provide something "not guaranteed
cryptographically strong, but at least not a complete nightmare".
Anyway, does this look like a reasonable approach?
-Phil
next prev parent reply other threads:[~2011-03-10 0:29 UTC|newest]
Thread overview: 5+ messages / expand[flat|nested] mbox.gz Atom feed top
2011-03-09 23:18 Mikael Magnusson
2011-03-10 0:29 ` Phil Pennock [this message]
2011-03-10 14:56 ` Bart Schaefer
2011-03-10 15:11 ` Mikael Magnusson
2011-03-17 3:44 ` Bart Schaefer
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=20110310002931.GA40551@redoubt.spodhuis.org \
--to=zsh-workers+phil.pennock@spodhuis.org \
--cc=mikachu@gmail.com \
--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).