First of all, this is just for fun, not really a practical method in my opinion, why do you want to code something which needs normally distributed random number in Bash in first place.

After my last post about how to use $RANDOM correctly, I thought it might be fun to see if I can make a normal random variable out of RANDOM.

I have done the similar thing in JavaScript, but clearly it would not be viable to code the same in Bash since it requires log or , those kinds of math functions, which Bash doesn’t have. There are some approximation methods or even you can use a predefined table, a technique I learned while programming microchips that has only tiny bits of memory and surely don’t have math library, either. Anyway, this is extremely impractical.

So, I remembered when I was at school, and the professor introduced us the Central limit theorem in probability course, he showed us some figures about how you can use a random variable to simulate a normal random variable just by summing up a certain amount of that random variable.

Interestingly, although the summation is a very simple operation, but I had never tried to make a simulation to see the result by my own hands until now, and I am doing it with Bash.

1   Result

Anyway, I modified the code from previous post to generate the figures. The first figure is using RANDOM, the number of summations is M=10.

http://farm4.staticflickr.com/3707/9281724222_c454e7d605_z.jpg

M=10

With the code below:


M=10
T=10
for ((i = 0; i < 32768 * T; i++)); do
R=0
for ((j = 0; j < M; j++)); do
((R += RANDOM))
done
echo $((R / M))
done |
./BashRANDOMNormal.py -M $M -T $T --stdin --save

The following figures show higher approximation degrees by increasing M.

http://farm8.staticflickr.com/7401/9278942029_195fb10a64_z.jpg

M=1

http://farm8.staticflickr.com/7374/9281724724_220364050c_z.jpg

M=2

http://farm4.staticflickr.com/3744/9278941785_23c28d455c_z.jpg

M=3

http://farm4.staticflickr.com/3744/9278941785_23c28d455c_z.jpg

M=5

http://farm4.staticflickr.com/3759/9281724320_9e795a57c4_z.jpg

M=10

http://farm6.staticflickr.com/5456/9278941293_001a95b3d4_z.jpg

M=100

Note that different M results different variance of simulated normal random variable. The variance of RANDOM is σRANDOM2=327682-112, and the variance of the simulated normal variable is σRANDOM2M. The figures above all have difference variances because of different M.

The summation of RANDOM results a 𝒩(327672,σRANDOM2M).

2   Conclusion

Even M=3 seems good enough to me, it still is impractical and kind of silly to code something like:


((N = (RANDOM + RANDOM + RANDOM) / 3))

If you need a normally distributed (Gaussian distribution) random number, use the right programming language. But if you like answering “Because I can,” then go ahead. I ain’t gonna stop you.