0
$\begingroup$

I'm a software engineer. I asked a friend for help easily creating a somewhat-normal distribution. This is just for the purpose of adding entropy to an application.

The code he gave me is this:

(rand(-1000, 1000) + rand(-1000, 1000) + rand(-1000, 1000)) / 100

If I run this 1000 times, it produces results between -27 and 28, with a good-enough normal distribution:

Mean: -0.671 Median: -1 Std Dev: 9.9

I'm not as good at math as I should be. Can anybody help me understand why this works?

  • 1
    The central limit theorem says that averages of random variables that are independent and identically distributed (with certain conditions on existence of moments) will approach the normal distribution as n gets large. In this case n is only 3 but the approximation is starting to work.2012-08-07

1 Answers 1

5

The central limit theorem says that if you sum enough similar random variables, you will get something that will be close to a normal law. Your solution (add 3 variables) is not really good, but it's a start.

There are far better solutions, like the Box–Muller transform, who will generate almost perfect normally distributed variables.

  • 3
    A _"poor man's normal random variable generator"_ sums $12$ random variables uniformly distributed on $(0,1)$, e.g. the results of $12$ calls to `rand()` in many computer languages, and subtracts $6$. This gives a number in $(-6,6)$ that can be taken as a sample of a $\mathcal N(0,1)$ random variable. The choice of $12$ samples allows elimination of a division to get the scaling correct: variance to be $1$. Of course, the Box-Muller transform gives better results.....2012-08-07