Assuming you can already produce variables of uniform distribution, you can produce variables of normal distribution using various formulas. Two of the most important are:
Most methods are based on Box-Muller method.
The Marsaglia method is my favorite since it does not require using sin() or cos() functions and the steps are very easy to implement.
Here is a sequence of the steps:
- Generate a value that follows uniform distribution in any space you want (eg: [ 0 , 1 ))
- Map that value to space (-1, +1) and assign it to variable U
- Repeat steps 1 and 2 and assign the result to variable V
- Calculate S = U*U + V*V
- if S = 0 or S >= 1 then free all variables if needed and restart from the beginning
- The following two variables will be independent and standard normally distributed (mean = 0, variance = 1):
Optionally you can add m to the quantities above to change the mean value of the distribution.