Search⌘ K

Introduction to Continuous Probability Distributions

Learn how to represent and implement continuous probability distributions in C# using the IDistribution interface. Discover how to generate samples, visualize distributions with histograms, and create derived distributions such as the normal distribution. This lesson helps you improve randomness handling by abstracting distributions for clearer, more powerful code.

In the previous lesson, we described our first attempt of fixing System.Random:

  • Make every method static and thread-safe
  • Draw a clear distinction between crypto-strength and pseudo-random methods

Continuous Probability Distribution

Let’s start by taking a step back and asking what we want when we’re calling NextInt or NextDouble or whatever on a source of randomness. What we’re saying is: we have a source of random Ts for some type T, and the values produced will correspond to some probability distribution. We can express that very clearly in the C# type system:

public interface IDistribution<T>
{
  T Sample();
}

In this lesson, we are going to look at continuous probability distributions, which we will represent as IDistribution<double>. We’ll look at integer distributions and other possible values for type parameter T in the upcoming lessons.

Now that we have an interface, what’s the simplest possible implementation? We have a library that produces a uniform distribution of doubles over the interval [0.0,1.0)[0.0, 1.0), which is called the standard continuous uniform distribution.

Here, the notation [x,y ...