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 $\mathrm{��}\left(\frac{32767}{2},\frac{{\sigma }_{\text{RANDOM}}^{2}}{M}\right)$.

## 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.
{"extraction_info": {"found_math": true, "script_math_tex": 0, "script_math_asciimath": 0, "math_annotations": 0, "math_alttext": 0, "mathml": 17, "mathjax_tag": 0, "mathjax_inline_tex": 0, "mathjax_display_tex": 0, "mathjax_asciimath": 0, "img_math": 0, "codecogs_latex": 0, "wp_latex": 0, "mimetex.cgi": 0, "/images/math/codecogs": 0, "mathtex.cgi": 0, "katex": 0, "math-container": 0, "wp-katex-eq": 0, "align": 0, "equation": 0, "x-ck12": 0, "texerror": 0, "math_score": 0.6030921339988708, "perplexity": 385.1267855980971}, "config": {"markdown_headings": true, "markdown_code": true, "boilerplate_config": {"ratio_threshold": 0.18, "absolute_threshold": 10, "end_threshold": 15, "enable": true}, "remove_buttons": true, "remove_image_figures": true, "remove_link_clusters": true, "table_config": {"min_rows": 2, "min_cols": 3, "format": "plain"}, "remove_chinese": true, "remove_edit_buttons": true, "extract_latex": true}, "warc_path": "s3://commoncrawl/crawl-data/CC-MAIN-2015-22/segments/1432207928562.33/warc/CC-MAIN-20150521113208-00174-ip-10-180-206-219.ec2.internal.warc.gz"}