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 $\sqrt{}$, 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$.

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$.

Note that different $M$ results different variance of simulated normal random variable. The variance of `RANDOM` is ${\sigma}_{\text{RANDOM}}^{2}=\frac{3276{8}^{2}-1}{12}$, and the variance of the simulated normal variable is $\frac{{\sigma}_{\text{RANDOM}}^{2}}{M}$. The figures above all have difference variances because of different $M$.

The summation of `RANDOM` results a $\mathcal{N}(\frac{32767}{2},\frac{{\sigma}_{\text{RANDOM}}^{2}}{M})$.

## 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.

## 0 comments:

## Post a Comment