This project has moved and is read-only. For the latest updates, please go here.

Best practises for generating random standard Normal deviates

Jun 18, 2011 at 1:13 PM


I use Math.NET Numerics' Normal class in order to generate millions of random standard normal deviates. I am not sure how to go about doing it.

Here is what I did:

From one normal instance:

 Normal n = new Normal();

I generate millions of random variates as follows:

double randGaussian = n.Sample();

Is there a better way of doing it?

Thanks in advance,


Jun 18, 2011 at 1:43 PM


The distributions have a Samples method that returns an IEnumerable<double>. You could do something like this to get an array of deviates:

var samples = new Normal().Samples().Take(1000000).ToArray();  //or ToList.

Or drop the ToArray and leave it as IEnumerable depending on what you are going to do with them.



Jun 18, 2011 at 1:52 PM
Edited Jun 18, 2011 at 1:53 PM

Thanks a lot  for your reply Marcus,

What about the RandomSource and WithMeanPrecision methods?

Can you briefly explain what they are for?

Thanks in advance,


Jun 18, 2011 at 2:13 PM
Edited Jun 18, 2011 at 2:13 PM

RandomSource  is the random number generator to use. It defaults to System.Random, but you can use any from MathNet.Numerics.Random.

WithMeanPrecision is a static method that creates a new Normal with the given mean and precision, where precision is 1/variance. 

You could do something like:

var normal = Normal.WithMeanPrecision(1, .2);
normal.RandomSource = new MersenneTwister();
var samples = normal.Samples().Take(1000000).ToArray();




Jun 18, 2011 at 2:25 PM

Thanks Marcus!