dotnetandsqldevelop dotnetandsqldevelop - 1 year ago 92
C# Question

Random double between given numbers

I'm looking for some succinct, modern C# code to generate a random double number between

. where the number should be

I'm thinking some solution that utilizes
somehow may make this more succinct.

Answer Source

You can easily define a method that returns a random number between two values:

private static readonly Random random = new Random();

private static double RandomNumberBetween(double minValue, double maxValue)
    var next = random.NextDouble();

    return minValue + (next * (maxValue - minValue));

You can then call this method with your desired values:

RandomNumberBetween(1.41421, 3.14159)
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download