random numbers

Ray Andrews <[email protected]>
Newsgroups gmane.comp.shells.zsh.user
Message-ID <[email protected]>
I was exploring Benford's Law thusly: (not very elegant but it works):

#!/bin/zsh

integer level=
integer a1=0
integer a2=0
integer a3=0
integer a4=0
integer a5=0
integer a6=0
integer a7=0
integer a8=0
integer a9=0

for ((level=1; level<=1000; level++)); do

#First tests:
var=$RANDOM

#Second tests:
#var=$(shuf -i 1-1000 -n 1)

     if [ ${var[1]} = '1' ]; then ((a1++)); fi
     if [ ${var[1]} = '2' ]; then ((a2++)); fi
     if [ ${var[1]} = '3' ]; then ((a3++)); fi
     if [ ${var[1]} = '4' ]; then ((a4++)); fi
     if [ ${var[1]} = '5' ]; then ((a5++)); fi
     if [ ${var[1]} = '6' ]; then ((a6++)); fi
     if [ ${var[1]} = '7' ]; then ((a7++)); fi
     if [ ${var[1]} = '8' ]; then ((a8++)); fi
     if [ ${var[1]} = '9' ]; then ((a9++)); fi
done

echo a1 is: $a1
echo a2 is: $a2
echo a3 is: $a3
echo a4 is: $a4
echo a5 is: $a5
echo a6 is: $a6
echo a7 is: $a7
echo a8 is: $a8
echo a9 is: $a9

---------------------------------------------------------------------------------

First tests give close to this:

5 /aWorking/Zsh/Source/Wk/Boneyard/Math 1 % . ./test1
a1 is: 339
a2 is: 336
a3 is: 134
a4 is: 27
a5 is: 31
a6 is: 25
a7 is: 28
a8 is: 40
a9 is: 40

Second tests close to this:

5 /aWorking/Zsh/Source/Wk/Boneyard/Math 1 % . ./test1
a1 is: 110
a2 is: 102
a3 is: 114
a4 is: 114
a5 is: 107
a6 is: 99
a7 is: 135
a8 is: 108
a9 is: 111

---------------------------------------------------------------------------------------------------

I think i know why, it's because zsh's RANDOM tops out at 32,767, but 
this insures that the first digit of a string of random numbers isn't 
random but strongly weighted to 1 and 2, with 3 also weighted.  Is this 
worth worrying about?  I'd say so.  Perhaps it should max out at 10,000 
or 100,000 or even offer a user selectable range?  Or do we have other 
tricks for coping with this?  I find zsh to be quite competent 
mathematically so having to use an external utility to get really random 
numbers seems not up to snuff.
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.