Random number within a range based on a normal distribution

ConfusedAgain picture ConfusedAgain · May 2, 2010 · Viewed 21.2k times · Source

I want to generate random numbers with a range (n to m, eg 100 to 150), but instead of purely random I want the results to be based on the normal distribution.

By this I mean that in general I want the numbers "clustered" around 125.

I've found this random number package that seems to have a lot of what I need: http://codeproject.com/KB/recipes/Random.aspx

It supports a variety of random generators (include mersiene twister) and can apply the generator to a distribution.

But I'm confused, if I use a normal distribution generator the random numbers are from roughly -6 to +8 (apparently the true range is float.min to float.max).

How do a scale that to my required range?

Answer

tzaman picture tzaman · May 2, 2010

A standard normal distribution has mean 0 and standard deviation of 1; if you want to make a distribution with mean m and deviation s, simply multiply by s and then add m. Since the normal distribution is theoretically infinite, you can't have a hard cap on your range e.g. (100 to 150) without explicitly rejecting numbers that fall outside of it, but with an appropriate choice of deviation you can be assured that (e.g.) 99% of your numbers will be within the range.

About 99.7% of a population is within +/- 3 standard deviations, so if you pick yours to be about (25/3), it should work well.

So you want something like: (normal * 8.333) + 125