Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Random number within a range based on a normal distribution

Tags:

.net

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?

like image 736
ConfusedAgain Avatar asked May 01 '10 23:05

ConfusedAgain


People also ask

How do you generate a random number in a normal distribution?

r = normrnd( mu , sigma ) generates a random number from the normal distribution with mean parameter mu and standard deviation parameter sigma . r = normrnd( mu , sigma , sz1,...,szN ) generates an array of normal random numbers, where sz1,...,szN indicates the size of each dimension.

How do I generate a random number from a normal distribution in Excel?

Formula SyntaxUse the formula "=NORMINV(RAND(),B2,C2)", where the RAND() function creates your probability, B2 provides your mean and C2 references your standard deviation. You can change B2 and C2 to reference different cells or enter the values into the formula itself.

What is normally distributed random numbers?

A distribution of values that cluster around an average (referred to as the “mean”) is known as a “normal” distribution. It is also called the Gaussian distribution (named for mathematician Carl Friedrich Gauss) or, if you are French, the Laplacian distribution (named for Pierre-Simon Laplace).

What type of random variable do we deal with normal distribution?

A normally distributed random variable may be called a “normal random variable” for short. We write X ∼ N ( μ , σ ) to mean that is a random variable that is normally distributed with mean and standard deviation .


2 Answers

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

like image 66
tzaman Avatar answered Sep 29 '22 08:09

tzaman


For the sake of interest, it's pretty straightforward to generate normally distributed random numbers from a uniform RNG (though it must be done in pairs):

Random rng = new Random(); double r = Math.Sqrt(-2 * Math.Log(rng.NextDouble())); double θ = 2 * Math.Pi * rng.NextDouble(); double x = r * Math.Cos(θ); double y = r * Math.Sin(θ); 

x and y now contain two independent, normally distributed random numbers with mean 0 and variance 1. You can scale and translate them as necessary to get the range you want (as interjay explains).


Explanation:

This method is called the Box–Muller transform. It uses the property of the two-dimensional unit Gaussian that the density value itself, p = exp(-r^2/2), is uniformly distributed between 0 and 1 (normalisation constant removed for simplicity).

Since you can generate such a value easily using a uniform RNG, you end up with a circular contour of radius r = sqrt(-2 * log(p)). You can then generate a second uniform random variate between 0 and 2*pi to give you an angle θ that defines a unique point on your circular contour. Finally, you can generate two i.i.d. normal random variates by transforming from polar coordinates (r, θ) back into cartesian coordinates (x, y).

This property – that p is uniformly distributed – doesn't hold for other dimensionalities, which is why you have to generate exactly two normal variates at a time.

like image 41
Will Vousden Avatar answered Sep 29 '22 06:09

Will Vousden