Each time you read the value of the variable $RANDOM
,
it gives you a random number between 0 and 2^15 - 1
,
that is 0 and 32767. So that doesn't give you enough range.
You could use two $RANDOM
as two digits of base-15,
and then take appropriate modulo and apply appropriate range normalization.
Here's the logic wrapped in a function:
randrange() {
min=$1
max=$2
((range = max - min))
((maxrand = 2**30))
((limit = maxrand - maxrand % range))
while true; do
((r = RANDOM * 2**15 + RANDOM))
((r < limit)) && break
done
((num = min + r % range))
echo $num
}
And then you can generate the files in a loop like this:
for i in 01 02 03 04 05; do
echo "AArandom=$(randrange 50000 150000)" > $i.dat
done
Note that there is a caveat in the implementation of randrange
:
there is a loop to re-roll in case the value would be biased,
but theoretically this may prevent the function from terminating.
In practice, that's extremely unlikely, but deserves a mention.