Generate random numbers following a normal distribution in C/C++

Damien picture Damien · Feb 24, 2010 · Viewed 191.6k times · Source

How can I easily generate random numbers following a normal distribution in C or C++?

I don't want any use of Boost.

I know that Knuth talks about this at length but I don't have his books at hand right now.

Answer

S.Lott picture S.Lott · Feb 24, 2010

There are many methods to generate Gaussian-distributed numbers from a regular RNG.

The Box-Muller transform is commonly used. It correctly produces values with a normal distribution. The math is easy. You generate two (uniform) random numbers, and by applying an formula to them, you get two normally distributed random numbers. Return one, and save the other for the next request for a random number.