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

The Box-Muller transform is what is commonly used. This correctly produces values with a normal distribution en.wikipedia.org/wiki/Normal_distributio... en.wikipedia.org/wiki/Box_Muller_transform The math is easy. You generate two uniform numbers and from those you get two normally distributed numbers.

Return one, save the other for the next request of a random number.

The Box-Muller transform is what is commonly used. This correctly produces values with a normal distribution. en.wikipedia.org/wiki/Normal_distributio... en.wikipedia.org/wiki/Box_Muller_transform The math is easy.

You generate two uniform numbers and from those you get two normally distributed numbers. Return one, save the other for the next request of a random number.

4 If you need speed, then the polar method is faster, though. And the Ziggurat algorithm even more (albeit much more complex to write). – Joey Feb 24 '10 at 12:15.

A quick and easy method is just to sum a number of evenly distributed random numbers and take their average. See the Central Limit Theorem for a full explanation of why this works.

1 Very interesting approach. Is it verified to really give normally distributed sub ensembles for smaller groups? – Morlock Feb 24 '10 at 12:53 2 @Morlock The larger the number of samples you average the closer you get to a Gaussian distribution.

If your application has strict requirements for the accuracy of the distribution then you might be better off using something more rigorous, like Box-Muller, but for many applications, e.g. Generating white noise for audio applications, you can get away with a fairly small number of averaged samples (e.g. 16). – Paul R Feb 24 '10 at 13:25 Plus, how do you parametrize this to get a certain amount of variance, say you want a mean of 10 with a standard deviation of 1? – Morlock Feb 24 '10 at 13:26 @Morlock: To get a given mean and SD you scale your underlying samples properly.

Generally, the underlying samples, u, are uniform over 0 to 1. Make them uniform over -1 to +1 (compute 2 you -1). You can then add the desired mean and multiply to get the desired standard deviation. – S.

Lott Feb 24 '10 at 13:39 @Morlock If you scale your PRNG to give random numbers in the range -1.0 to +1.0 then you get a mean of 0.0 and a variance of 0.5 when you take a sufficiently large average. You can just linearly shift and scale either the input numbers or the output average to get whatever mean and variance you need. – Paul R Feb 24 '10 at 13:39.

Use std::tr1::normal_distribution. The std::tr1 namespace is not a part of boost. It's the namespace that contains the library additions from the C++ Technical Report 1 and is available in up to date Microsoft compilers and gcc, independently of boost.

TR1 is not boost. – Joe Gauterin Feb 24 '10 at 11:31 TR1 is not standard, either. – anon Feb 24 '10 at 11:39 11 He didn't ask for standard, he asked for 'not boost'.

– Joe Gauterin Feb 24 '10 at 11:43.

Here are some solutions ordered by ascending complexity. Add 12 uniform numbers from 0-1 and subtract 6. This will match mean and standard deviation of a normale variable.An obvious drawback is that the range is limited to +/-6 - unlike a true normal distribution.

Box-Muller transform - was listed above, and is relatively simple to implement. If you need very precise samples however, be aware that the Box-Muller transform combined with some uniform generators suffers from an anomaly called Neave Effect.H.R.Neave, “On using the Box-Muller transformation with multiplicative congruential pseudorandom number generators,” Applied Statistics, 22, 92-97, 1973 For best precision I suggest drawing uniforms and applying the inverse cumulative normal distribution to arrive at normal distributed variates. You can find a very good algorithm for the inverse cumulative normal distribution at home.online.no/~pjacklam/notes/invnorm/#... Hope that helps Peter.

Thank you – stonybrooknick Nov 2 at 13:30 1 @stonybrooknick The original reference is added. Cool remark: While googling "box muller neave" to find the reference, this very stackoverflow question came up on the first result page! – Peter G.

Nov 2 at 17:18 yeah its not every well known outside certain small communities and interest groups – stonybrooknick Nov 4 at 18:55.

You can use the GSL. Some complete examples are given to demonstrate how to use it.

I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.

Related Questions