mochahochabeachside.com

Testing Random Randomness…

by on Feb.10, 2009, under Scripts

A post at Manik 2.0 got me thinking on whether or not the $RANDOM variable in bash was really all that random.  So, I created a little test script.  It's quite simple.  It accepts a command line argument for number of loops. Then, each loop, it assigns a random number to $RANDTEST, and then compares $RANDTEST to $RANDOM. Since $RANDOM changes every time, it should never, ever match.

There are two primary schools of thought about this... One statest that $RANDOM is random at all times.  Another than $RANDOM is not random enough, unless seeded.  Therefore, I've created this script to test on both schools of thought.  To test with $RANDOM seeded, uncommend the second line. This seeds $RANDOM off of the pid of the script. Otherwise, $RANDOM is untested.

#!/bin/bash
#RANDOM=$$
RANDTEST=0
DOWHILE=1
LIMIT=$1
while [ "${DOWHILE}" -lt "${LIMIT}" ]
do
RANDTEST=${RANDOM}
if [ "${RANDTEST}" -eq "${RANDOM}" ]
then
echo "MATCH! (...on ${DOWHILE})"
fi
let "DOWHILE = ${DOWHILE} + 1"
done

I haven't had the time to put it through serious paces, but tonight I'll open a terminal on my desktop (ubuntu), and let it run for a few million loops.  We'll see what comes up.

:,
No comments for this entry yet...

Leave a Reply

Looking for something?

Use the form below to search the site:

Still not finding what you're looking for? Drop a comment on a post or contact us so we can take care of it!

Visit our friends!

A few highly recommended friends...