Skip to main content
Updated Code sample link
Source Link
Darrel K.
  • 1.7k
  • 21
  • 29

For future readers if you want a random number in a range use the following code:

public double GetRandomNumberInRange(Random random,double minNumber, double maxNumber) { return random.NextDouble() * (maxNumber - minNumber) + minNumber; } 

usage:

Random r = new Random(); double num1 = GetRandomNumberInRange(r, 50, 100) 

C# Random double between min and max

Code sampleCode sample

For future readers if you want a random number in a range use the following code:

public double GetRandomNumberInRange(Random random,double minNumber, double maxNumber) { return random.NextDouble() * (maxNumber - minNumber) + minNumber; } 

usage:

Random r = new Random(); double num1 = GetRandomNumberInRange(r, 50, 100) 

C# Random double between min and max

Code sample

For future readers if you want a random number in a range use the following code:

public double GetRandomNumberInRange(Random random,double minNumber, double maxNumber) { return random.NextDouble() * (maxNumber - minNumber) + minNumber; } 

usage:

Random r = new Random(); double num1 = GetRandomNumberInRange(r, 50, 100) 

C# Random double between min and max

Code sample

Moved new Random instance out of method to save memory allocation.
Source Link
Darrel K.
  • 1.7k
  • 21
  • 29

For future readers if you want a random number in a range use the following code:

public double GetRandomNumberInRange(Random random,double minNumber, double maxNumber) { return new Random()random.NextDouble() * (maxNumber - minNumber) + minNumber; } 

usage:

Random r = new Random(); double num1 = GetRandomNumberInRange(r, 50, 100) 

C# Random double between min and max

Code sample

For future readers if you want a random number in a range use the following code:

public double GetRandomNumberInRange(double minNumber, double maxNumber) { return new Random().NextDouble() * (maxNumber - minNumber) + minNumber; } 

C# Random double between min and max

Code sample

For future readers if you want a random number in a range use the following code:

public double GetRandomNumberInRange(Random random,double minNumber, double maxNumber) { return random.NextDouble() * (maxNumber - minNumber) + minNumber; } 

usage:

Random r = new Random(); double num1 = GetRandomNumberInRange(r, 50, 100) 

C# Random double between min and max

Code sample

Updated after comment made. Wrong order of variables.
Source Link
Darrel K.
  • 1.7k
  • 21
  • 29

For future readers if you want a random number in a range use the following code:

public double GetRandomNumberInRange(double minNumber, double maxNumber) { return new Random().NextDouble() * (minNumbermaxNumber - maxNumberminNumber) + minNumber; } 

C# Random double between min and max

Code sample

For future readers if you want a random number in a range use the following code:

public double GetRandomNumberInRange(double minNumber, double maxNumber) { return new Random().NextDouble() * (minNumber - maxNumber) + minNumber; } 

C# Random double between min and max

For future readers if you want a random number in a range use the following code:

public double GetRandomNumberInRange(double minNumber, double maxNumber) { return new Random().NextDouble() * (maxNumber - minNumber) + minNumber; } 

C# Random double between min and max

Code sample

Source Link
Darrel K.
  • 1.7k
  • 21
  • 29
Loading