Pure Programmer
Blue Matrix


Cluster Map

Project: Central Limit Theorem

The [[Central_limit_theorem#Classical_CLT|Central Limit Theorem]] states that if we take random samples of size N from any distribution of independent random variables, that the distribution of sample averages (`bar X`) should fall in a normal distribution regardless of the type of distribution of the samples. For example our typical random number generators use a uniform, not normal, distribution. But if we take samples of size N, average them, then we should get a sample distribution that is normal with a mean that is the same as our sample source distribution mean and a standard deviation that is equal to the standard deviation of our sample source distribution divided by `sqrt(N)`.

Write a program that accepts a sample size N on the command line and the number of samples to collect M. Then generate the mean (`bar X`) of a sample of size N from random float values [0, 20). Do this M times maintaining a running count of the occurances in an array of 20 by using `lfloor bar X rfloor` (use floor() or int() function) to select the array element to increment. Output a tab-delimited table with the array index in column 1 and the count in column 2. This output can easily be piped into the Histogram project to get a simple graph that should look like a normal distribution centered on 10.0. Notice how as the sample size increases, the standard deviation shrinks?

Output
$ g++ -std=c++17 CentralLimitTheorem.cpp -o CentralLimitTheorem -lfmt $ ./CentralLimitTheorem 3 1000 0 0 1 6 2 10 3 19 4 25 5 43 6 68 7 95 8 98 9 105 10 110 11 105 12 97 13 72 14 65 15 45 16 18 17 12 18 5 19 2 $ g++ -std=c++17 CentralLimitTheorem.cpp -o CentralLimitTheorem -lfmt $ ./CentralLimitTheorem 5 1000 0 0 1 0 2 4 3 5 4 11 5 29 6 59 7 113 8 138 9 140 10 142 11 142 12 103 13 65 14 31 15 10 16 6 17 2 18 0 19 0 $ g++ -std=c++17 CentralLimitTheorem.cpp -o CentralLimitTheorem -lfmt $ ./CentralLimitTheorem 10 1000 0 0 1 0 2 0 3 0 4 5 5 9 6 39 7 90 8 157 9 205 10 194 11 141 12 99 13 45 14 13 15 3 16 0 17 0 18 0 19 0 $ g++ -std=c++17 CentralLimitTheorem.cpp -o CentralLimitTheorem -lfmt $ ./CentralLimitTheorem 25 1000 0 0 1 0 2 0 3 0 4 0 5 0 6 6 7 38 8 144 9 288 10 331 11 141 12 49 13 3 14 0 15 0 16 0 17 0 18 0 19 0

Solution