Joseph C. Tuttle wrote:
> Is there a simple way in Linux to generate a series of three hundred to
> four hundred unique random four digit numbers? I haven't done enough
> programming to write my own random number generator, but I do know Linux
> can generate them for its own uses. I have searched man and info pages,
> and the "Linux Complete Command Reference," and found tools for
> programmers to use to generate random numbers, but that was all I could
> find. Is there a simple way for me to do this?
>
> Any help would be appreciated.
I've seen a couple of programmer-centric responses, but the implication
I draw from the phrasing of the request is that Joseph is interested in
a somewhat more simple solution than writing his own program. Nobody
yet has mentioned that both bash and ksh provide a value in the
environment variable RANDOM.
Ksh specifically implies that this is generated using the rand(3) by
referring the reader to it; bash doesn't say. Both claim that if you
assign a value to RANDOM, you'll initialize the random number series.
For instance, the following shellscript, in either bash or ksh, will
display a series of random numbers:
for [ EVER ]
do
echo "Random value: $RANDOM";
sleep 1;
done;
If you wanted to save a set of 1000 random numbers:
COUNTER=0
while [ "$COUNTER" -le 1000 ]
do
echo $RANDOM >> random.out
let COUNTER=$COUNTER+1
done;
To specifically get 400 random, 4-digit numbers in a file in the current
directory called random.out:
COUNTER=0
while [ "$COUNTER" -le 400 ]
do
TMPRAND=$RANDOM
if [ `expr "$TMPRAND" : ".*"` -eq 4 ]
then
echo $TMPRAND >> random.out
let COUNTER=$COUNTER+1
fi;
done;
Depending on WHY you want random numbers, this will probably suffice for
your purposes, Joseph. But please read the following section anyway.
RANDOM NUMBER CAVEATS
=====================
Random numbers aren't, at least on computers. Rather, they're correctly
called pseudo-random numbers. They're supposed to be random within a
series, and shouldn't repeat; but the series can be recreated if the
starting "seed" to the random number generator package, like rand(3), is
set to a known value.
This can be seen by setting RANDOM=0 in ksh, looking at the series put
out, then resetting it to 0 again and comparing the output:
RANDOM=0
COUNTER=0
while [ "$COUNTER" -le 50 ]
do
echo $RANDOM >> foobar
let COUNTER=$COUNTER+1
done
RANDOM=0
COUNTER=0
while [ "$COUNTER" -le 50 ]
do
echo $RANDOM >> barfoo
let COUNTER=$COUNTER+1
done
diff foobar barfoo
Under both bash and ksh, the two files will be identical.
This isn't usually of importance, unless you are REALLY serious about
wanting TRULY random numbers; or you don't want anyone to be able to
recreate your series, even if they could figure out your seed.
BASH AND KSH CAVEATS
====================
It seems that the two shells certainly don't use the same method
of generating the random number sequence. Setting the RANDOM variable to
the same seed value--say, 0--and running a sequence as above from the two
shells gives different sequences.
Cheers,
--
Dave Ihnat
[EMAIL PROTECTED] || [EMAIL PROTECTED]
312/315.1075 [home office] || 312/443.5860 [office]
--
PLEASE read the Red Hat FAQ, Tips, Errata and the MAILING LIST ARCHIVES!
http://www.redhat.com/RedHat-FAQ /RedHat-Errata /RedHat-Tips /mailing-lists
To unsubscribe: mail [EMAIL PROTECTED] with
"unsubscribe" as the Subject.