In this article, we are going to learn how to generate random double numbers in a range in C#.

To download the source code for this article, you can visit our GitHub repository.

Generate a Pseudorandom Double

To generate a pseudorandom double number we can use the NextDouble method from the Random class:

var random = new Random();
var rDouble = random.NextDouble();

This will return a double that is at least 0.0 and less than 1.0.

Generate a Secure Random Double

The RandomNumberGenerator class does not have a built-in method to generate a secure random double. In order to get a double between 0.0 and 1.0, we need to first create a secure random generated integer with the GetInt32 method:

RandomNumberGenerator.Create(); 
var denominator = RandomNumberGenerator.GetInt32(2, int.MaxValue);

The lower bound passed to GetInt32 must be greater than 0 to avoid the chance of the method returning a 0 causing a divide by zero error later when we are converting from integer to double. Additionally, if we wish the double to not have the possibility of equaling 1, then the lower bound passed to GetInt32 must be 2 or greater.

Then we can divide 1 by the secure random integer we just created and cast the result to a double to generate a secure random double:

public static double GetSecureDouble()
{
    RandomNumberGenerator.Create();
    var denominator = RandomNumberGenerator.GetInt32(2, int.MaxValue);
    double sDouble = (double) 1 / denominator;
    return sDouble;
}

The double’s length and value will be determined by the upper and lower bound criteria given to the RandomNumberGenerator.GetInt32 method.

Generate a Pseudorandom Double Within a Specified Range

The Random class provides a simple way to generate a double between 0 and 1. However, we want to get a number larger than 1 or less than zero within a custom range. Let’s accomplish this with our own custom method:

public static double GetPseudoDoubleWithinRange(double lowerBound, double upperBound)
{
    var random = new Random();
    var rDouble = random.NextDouble();
    var rRangeDouble = rDouble * (upperBound - lowerBound) + lowerBound;
    return rRangeDouble;
}

We begin our custom method by generating a double with the NextDouble method. Notice that next we use a specific formula that modifies the double into the specified range. Multiplying the double value by the difference between the lowerBound and upperbound ensures that the double is below the upper bound.  Then we add the lowerBound which keeps the double above the lower bound.

Generate a Secure Random Double Within a Specified Range

We can create a secure double using the same formula and utilizing our custom GetSecureDouble method:

public static double GetSecureDoubleWithinRange(double lowerBound, double upperBound)
{
    var rDouble = GetSecureDouble();
    var rRangeDouble = (double) rDouble * (upperBound - lowerBound) + lowerBound;
    return rRangeDouble;
}

Now, if we want to use our custom methods, we can call them in the same way we would as the methods within the Random class:

var pseduoDoubleWithinRange = GetPseudoDoubleWithinRange(16, 30000);
var secureDoubleWithinRange = GetSecureDoubleWithinRange(4, 123000);

That’s it, let’s summarize what we’ve learned.

Conclusion

The two main ways to generate random double numbers in a range in C# is using the Random and RandomNumberGenerator classes.  At first glance, it could appear slightly overwhelming when attempting to generate a double within a range.  However, we hope that the above explanations clearly show how you can create your own methods to obtain your required random results.

If you want to achieve the same thing with integers, you can visit our article on how to generate random integers in C#.

Wanna join Code Maze Team, help us produce more awesome .NET/C# content and get paid? >> JOIN US! <<