Skip to main content
GameDev.net gamedev.net
🔒 Locked

Sample from Normal Distrabution

Started by MetaKnight Jul 13, 2004 at 1:03 PM 4 replies 800+ views
Original Post
MetaKnight
MetaKnight
Hi, i was wondering how to get a random sample from a normal distrabution? I know the function of a normal bell curve, but i was wondering how i could get a random sample, just like how my calculator does it. Thanks
SiCrane
SiCrane
My first instinct would be to use boost::random. The source code for their normal distribution is also freely available if you're interested in the underlying algorithm and not just application.
SpaceDude
SpaceDude
Here you go... you just need to supply a random number between 0 and 1, the function then returns a normally distributed number. Multiply by your standard deviation and add the mean.

http://home.online.no/~pjacklam/notes/invnorm/
MetaKnight
MetaKnight
twisted! yeah that's just what i wanted thanks
higherspeed
higherspeed
A value from the normal distribtion is unbounded, so choosing a number between 0 and 1 can't be right. If want to generate the value from a uniform distribution then you take the sum of repeated samples, then divide by root n(n being the number of samples) and you'll be left with a normal distribution of some description, depending on the initial bounds of the uniform distribution.

I could be wrong about root n, but I don't think I am. This is all an application of the central limit theorem.

http://mathworld.wolfram.com/CentralLimitTheorem.html

There will ofcourse be far better ways of generating it, so a library is probably the thing to use.
Lutz
Lutz
Easy:

Set a and r to be two random numbers between 0 and 1.
Then

x = sqrt(-2*log(r)) * sin(2*pi*a)

is normally distributed with variance 1 and mean value 0.

Lutz

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.