This project has moved. For the latest updates, please go here.

Normal Distribution

Feb 18, 2010 at 4:41 PM

Hi,

I'm trying to use the Normal Distribution to generate random numbers.

I'm using N(0,1).

The return value ou the function sample() is the F.D.P [f(x)] or the x value correspondent?

f(x) = \frac{1}{\sqrt{2\pi}} \, \exp\left(-\frac{x^2}{2} \right).

I appreciate all the help you can give me!

 

Best Regards

 

Gabriel Blanco

Coordinator
Feb 18, 2010 at 6:58 PM

Hi Gabriel,

You essentially have two options:

  1. You create a normal distribution instance using "var n = new Normal();" or "var n = new Normal(0.0,1.0)" [The first one defaults to the standard normal with mean 0 and sdev 1. Then you can call n.Sample().]
  2. You call a static method "Normal.Sample(rnd,0.0,1.0)" which generates normally distributed random variables. In this case "rnd" is a random number generator: either a "new System.Random" or a "new MathNet.Numerics.Random.Xxx"

If you want the find the density of a normal distribution you should create "var n = Normal(); n.Density(x)" to get the density at the point x.

Hope this helps. Let me know if you have other questions.

Jurgen

Feb 18, 2010 at 7:06 PM
Hi, 

Thank you! What in fact I am trying to do is to generate random numbers between two values, but I want that the values in the middle are to be selected more often. That's why I thought of Normal Distribution.

I'm not sure, if I was clear enough?!

This is my code, using your explanation:


private
static float RandomNormalDistribution(float min, float max) { bool validity = false; float returnValue = 0.0f; while (!validity) { returnValue = (float)Normal.Sample(random, 0, 1); if (returnValue >= -4.0f && returnValue <= 4.0f) { validity = true; } } float valueShift = returnValue + 4.0f; float percentage = valueShift * 100.0f / 8.0f; return min + (max - min) * percentage * 0.01f; }
I was wondering if you can give your opinition. Am I using the sample function correctly?
Thank your very much!
Gabriel Blanco

private static float RandomNormalDistribution(float min, float max)
        {
            bool validity = false;
            float returnValue = 0.0f;
            while (!validity)
            {
                returnValue = (float)Normal.Sample(random, 0, 1);                
                if (returnValue >= -4.0f && returnValue <= 4.0f)
                {
                    validity = true;
                }
            }
            float valueShift = returnValue + 4.0f;
            float percentage = valueShift * 100.0f / 8.0f;
            return min + (max - min) * percentage * 0.01f;
        }

 

 

Coordinator
Feb 18, 2010 at 7:17 PM

Hi Gabriel,

You are definitely using the Sample function correctly. Your samples will come from a normal distribution truncated at [-4,4]; if this is the type of distribution you want it looks OK. Another distribution that you could look into is the Beta distribution. This is also supported by Math.Net and has finite support. It looks a bit like a truncated normal (for a particular choice of parameters).

Hope this helps.

Jurgen

 

Feb 18, 2010 at 7:19 PM

Hi again!

Thank you very much for your help!

Best Regards

Gabriel Blanco