[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Re: [Help-bash] $RANDOM excludes value zero when called in a subscript
From: |
Roger Price |
Subject: |
Re: [Help-bash] $RANDOM excludes value zero when called in a subscript |
Date: |
Tue, 6 Aug 2019 22:07:12 +0200 (CEST) |
User-agent: |
Alpine 2.20 (LSU 67 2015-01-07) |
On Tue, 6 Aug 2019, Greg Wooledge wrote:
First mistake is making us go to a web page to retrive the script.
This is contrary to the advice given in other mailing lists, and is not
mentioned in the page https://lists.gnu.org/mailman/listinfo/help-bash
Second mistake:
declare a HIST3 # Histogram of $RANDOM in sub-script
Pretty sure you meant to use "-a" there, not "a".
Yes, clearly a mistake.
Third mistake: ... one, using a static name in a publicly writable directory,
and two, expecting to be able to execute a program in /tmp, which may or may
not be mounted with noexec permissions.
Agreed for production code, but for a quick demonstration, I take the risk.
OK, now let's take a look at the actual meat of the issue.
R3=$( $SS ) # $RANDOM in sub-script
So, you're executing this script repeatedly in a tight loop. You didn't
seed the random number generator, so it's going to use whatever its
fallback seeding strategy is, which is not documented, ...
The real stumper is that you didn't find *lots* more issues with the
randomness, using the default seeding of $RANDOM in a tight loop. I'm
amazed you're not just getting the same value every single time.
I'm surprized - I was assuming that $RANDOM is based on an algorithm, not on
external entropy. If indeed $RANDOM uses, and can run out of external entropy,
then why doesn't it stop and wait, or issue an error message? Is $RANDOM
purely algorithmic, or not?
...then you really need to understand the care and feeding of a linear
congruential pseudorandom number generator.
Your terminology makes me suspect that $RANDOM is of the form
X_{n+1} = (a X_n + b) mod m
with X_0 based on (tv.tv_sec ^ tv.tv_usec ^ getpid ()) or whatever value is
assigned to RANDOM.
Whatever you're really doing, you need to design it so that it uses a
single instance of a PRNG,
I reduced my test script to one single use of $RANDOM and still saw the same
anomaly, so it's not caused by multiple instances.
Knowing more about your actual project and why you're trying to farm
out the $RANDOM stuff would help us help you design it properly.
My use case was originally very simple - pseudo random integers in the interval
10..20 to drive a sleep command which slows down network intensive activity and
plays nice with someone else's server. While debugging this I came across the
anomaly, and having done a lot of simulations with algorithmic RNG's I became
curious.
I read your message carefully, several times, but I see no explanation for why
$RANDOM does not issue zero values from a sub-script.
Roger