Today’s a fairly short post, all about building a random distribution in .NET, both C# and F#. There are a couple of ways to do it, one which involves taking a reference and one which does not.

## Taking a Reference: the Math.Net Approach

The Math.Net library is an excellent library for anybody doing serious numeric computation in .NET. You can get the library from NuGet and incorporate it rather easily in your applications. And without putting too fine a point on it, it’s a serious math project because there’s good F# support. Anybody who knows just how much of an F# apologist I am might roll their eyes, but I am actually being serious here. F# has a stronger reputation in the scientific programming community than in the world of general programming, and having explicit support rather than “We developed it for C# and it’ll use the same BCL classes in the end” caters to that audience.

Anyhow, if you import the MathNet.Numerics package and open MathNet.Numerics.Distributions, you’ll find a Normal() method which is quite easy to use:

```
var rng = new Normal(31.0, 6.48);
var rn = rng.Sample();
var lotsOfRns = new double[1000];
rng.Sample(lotsOfRns);
```

Here, we create a new normal distribution with a mean of 31.0 and standard deviation of 6.48, calling it `rng`

. I then generate one sample from this distribution, calling it `rn`

. I can return `rn`

, which is of type `double`

, if I need a single one. Alternatively, if I want lots of them, I can create an array of some length and call `Sample()`

and pass in the array. The `Sample()`

method will fill the array with values drawn from the distribution.

It’s all quite easy to use and cross-platform. If you’re in need of some data following a normal distribution, it’s hard to beat this. But sometimes you can’t take a reference.

## Can’t Take a Reference: C# Edition

For a Microsoft Cloud Workshop, I needed to generate data following a normal distribution, but didn’t want to include Math.Net. Fortunately, we can estimate a normal distribution using a concept called the Box-Muller transform. This is an estimation of a normal distribution, but it will do pretty well. Here’s an example of the method in C#:

```
private static double BoxMullerTransformation(Random rand, double mean, double stdDev)
{
double u1 = 1.0 - rand.NextDouble();
double u2 = 1.0 - rand.NextDouble();
double randStdNormal = Math.Sqrt(-2.0 * Math.Log(u1)) * Math.Sin(2.0 * Math.PI * u2);
return mean + stdDev * randStdNormal;
}
```

This relies solely on the System library, so no additional dependencies.

## Can’t Take a Reference: F# Edition

The last example for today is the same formula, but this time in F#.

```
let boxMullerTransformation (rand:Random) mean stdev =
let u1 = 1.0 - rand.NextDouble()
let u2 = 1.0 - rand.NextDouble()
let randStdNormal = Math.Sqrt(-2.0 * Math.Log(u1)) * Math.Sin(2.0 * Math.PI * u2)
mean + stdev * randStdNormal
```

As you can see, I’m not doing anything particularly exciting here. It’s basically the same as the C# code, but that’s okay—not all F# code has to look different from its C# counterparts!

## Conclusion

Today’s post was a pretty short one and acts as battlespace preparation. In the next post, I’m going to do some fun things with data pulled from this approximately normal distribution.

## One thought on “Estimating a Random Distribution in .NET”