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.
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.
Anyway, I modified the code from previous post to generate the figures. The first figure is using RANDOM, the number of summations is .
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 .
Note that different results different variance of simulated normal random variable. The variance of RANDOM is , and the variance of the simulated normal variable is . The figures above all have difference variances because of different .
The summation of RANDOM results a .
Even 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.