views:

1023

answers:

6

I would like to create a function that accepts Double mean, Double deviation and returns a random number with a normal distribution.

Example: if I pass in 5.00 as the mean and 2.00 as the deviation, 68% of the time I will get a number between 3.00 and 7.00

My statistics is a little weak…. Anyone have an idea how I should approach this? My implementation will be C# 2.0 but feel free to answer in your language of choice as long as the math functions are standard.

I think this might actually be what I am looking for. Any help converting this to code?

Thanks in advance for your help.

+7  A: 

You might be interested in Math.NET, specifically the Numerics package.

Caveat: The numerics package targets .NET 3.5. You may need to use the Iridium package if you are targeting an earlier version...

Jason Punyon
Looking at the feature at http://www.mathdotnet.com/doc/IridiumFeatures.ashx now... Not quite sure which function I want. Maybe one of the Continuous Probability Distributions? I might just have to download the source and pour over the code tonight.
J.Hendrix
You want to use MathNet.Numerics.Distributions, and do something like that, which will draw from a Normal with mean 5.0 and sigma 0.68: var mu = 5.00; var sigma = 0.68; var normal = new NormalDistribution(mu, sigma); var draw = normal.NextDouble();
Mathias
+3  A: 

This library is pretty good also:

.NET random number generators and distributions

locster
+1 This looks promising. Thanks
J.Hendrix
+1  A: 

Sorry I don't have any code for you but I can point you to some algorithms. See this page: http://en.wikipedia.org/wiki/Normal%5Fdistribution#Generating%5Fvalues%5Ffor%5Fnormal%5Frandom%5Fvariables

The algorithm you choose I guess depends on how accurate you want it and how fast it needs to be.

Martin Sherburn
+1 This looks good too. How do I create the "two independent random numbers U and V uniformly distributed on (0, 1]"? Sorry... I told you my stats are weak.
J.Hendrix
In C# you would use the "Random" class (http://msdn.microsoft.com/en-us/library/system.random.aspx). Specifically the method NextDouble returns a uniformly distributed number in the range of 0 to 1. Uniformly distributed just means that you have an equal chance of getting any of the numbers in the range, there is no bias towards any specific number.
Martin Sherburn
A: 

The MetaNumerics library, also .NET, will calculate a normal distribution (and just about anything else from statistics), super quickly. Have a look at the Feature's page for more details. The Codeplex page is here: http://metanumerics.codeplex.com/.

fatcat1111
+3  A: 

See this CodeProject article: Simple Random Number Generation. The code is very short, and it generates samples from uniform, normal, and exponential distributions.

John D. Cook
+1 I think this may be exactly what I am looking for! Thanks.
J.Hendrix
Thanks, this perfect. Not too long and does exactly what I want it to do.
J.Hendrix
+4  A: 

Here is some C that returns two values (rand1 and rand2), just because the algorithm efficiently does so. It is the polar form of the Box-Muller transform.

void RandVal (double mean1, double sigma1, double *rand1, double mean2, double sigma2, double *rand2)
{
double u1, u2, v1, v2, s, z1, z2;

do {
    u1 = Random (0., 1.);  // a uniform random number from 0 to 1
    u2 = Random (0., 1.);
    v1 = 2.*u1 - 1.;
    v2 = 2.*u2 - 1.;
    s = v1*v1 + v2*v2;
} while (s > 1. || s==0.); 

z1 = sqrt (-2.*log(s)/s)*v1;
z2 = sqrt (-2.*log(s)/s)*v2;
*rand1 = (z1*sigma1 + mean1);
*rand2 = (z2*sigma2 + mean2);
return;

}

It would be helpful if you named your variables something other than "u", "v", etc., for example, mean and sigma are very helpful.
alord1689