Pure Programmer

## 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
$javac -Xlint CentralLimitTheorem.java$ java -ea CentralLimitTheorem 3 1000 0 2 1 2 2 7 3 19 4 41 5 63 6 71 7 94 8 90 9 117 10 117 11 99 12 93 13 63 14 60 15 32 16 20 17 9 18 1 19 0 $javac -Xlint CentralLimitTheorem.java$ java -ea CentralLimitTheorem 5 1000 0 0 1 1 2 0 3 4 4 22 5 33 6 68 7 85 8 126 9 153 10 157 11 130 12 100 13 64 14 31 15 21 16 4 17 1 18 0 19 0 $javac -Xlint CentralLimitTheorem.java$ java -ea CentralLimitTheorem 10 1000 0 0 1 0 2 0 3 0 4 4 5 16 6 38 7 94 8 155 9 186 10 199 11 150 12 106 13 35 14 16 15 1 16 0 17 0 18 0 19 0 $javac -Xlint CentralLimitTheorem.java$ java -ea CentralLimitTheorem 25 1000 0 0 1 0 2 0 3 0 4 0 5 0 6 6 7 45 8 137 9 309 10 309 11 155 12 35 13 3 14 1 15 0 16 0 17 0 18 0 19 0

Solution