I need to use normally distributed random numbers in my program. Let's say I need to generate x (size is R^(m x n) ) whose elements are from the Gaussian distribution of zero mean and one variance. How do I do this in C with good accuracy?
Asked
Active
Viewed 1,431 times
3
-
[This answer provides C code](http://stackoverflow.com/a/23609868/211160), FWIW. – HostileFork says dont trust SE Oct 24 '14 at 18:31
-
http://c-faq.com/lib/gaussian.html – Grzegorz Szpetkowski Oct 24 '14 at 18:37
-
[Box-Muller Transform](http://en.wikipedia.org/wiki/Box–Muller_transform) is a common way of doing it. – Dietrich Epp Oct 24 '14 at 18:46
1 Answers
1
There are a lot of algorithms to produce a normal distribution if you have a uniform random generator. Have a look at this section of the wikipedia article. One possible approach is by using the central limit theorem. The approach here would be to generate many integer values with uniform distribution and then compute their arithmetic mean.

Ivaylo Strandjev
- 69,226
- 18
- 123
- 176
-
The most practical approach? It takes a dozen uniform samples to get a single variable that's at least roughly normal distributed, and far more to get better approximations. It's not like the Box-Muller method is particularly complicated. It only needs square root, logarithm, and trigonometry - i.e. `libm`. Probably not even more lines of code. – Oct 24 '14 at 18:41
-
@delnan actually I did not read all the suggested ideas. I have simply done something similar with central limit theorem. I will re-phrase the answer. – Ivaylo Strandjev Oct 24 '14 at 18:42