Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
Random.NextDouble() Method in C#
The Random.NextDouble() method in C# generates a random floating-point number that is greater than or equal to 0.0 and less than 1.0. This method is useful for generating decimal probabilities, percentages, or scaling random values to specific ranges.
Syntax
Following is the syntax for the NextDouble() method −
public virtual double NextDouble();
Return Value
The method returns a double value in the range [0.0, 1.0), where 0.0 is inclusive and 1.0 is exclusive.
Using NextDouble() for Basic Random Numbers
Example
using System;
public class Demo {
public static void Main() {
Random r = new Random();
Console.WriteLine("Random floating point numbers...");
for (int i = 0; i < 5; i++) {
double randomValue = r.NextDouble();
Console.WriteLine("Value " + (i + 1) + ": " + randomValue);
}
}
}
The output of the above code is −
Random floating point numbers... Value 1: 0.93591266727816 Value 2: 0.36406785872023 Value 3: 0.122396959514542 Value 4: 0.795166163144245 Value 5: 0.954394097884369
Using NextDouble() for Scaling to Custom Ranges
You can scale the output of NextDouble() to generate random numbers within specific ranges by using mathematical operations −
Example
using System;
public class Demo {
public static void Main() {
Random r = new Random();
Console.WriteLine("Scaled random numbers:");
// Random number between 10 and 20
double range1 = r.NextDouble() * 10 + 10;
Console.WriteLine("Between 10-20: " + Math.Round(range1, 2));
// Random percentage (0-100)
double percentage = r.NextDouble() * 100;
Console.WriteLine("Percentage: " + Math.Round(percentage, 2) + "%");
// Random number between -5 and 5
double range2 = r.NextDouble() * 10 - 5;
Console.WriteLine("Between -5 to 5: " + Math.Round(range2, 2));
}
}
The output of the above code is −
Scaled random numbers: Between 10-20: 17.24 Percentage: 64.83% Between -5 to 5: 2.15
Using NextDouble() for Distribution Analysis
Example
using System;
public class Demo {
public static void Main() {
int[] distribution = new int[5];
Random r = new Random();
// Generate 1000 random numbers and categorize them
for (int i = 0; i < 1000; i++) {
double d = r.NextDouble();
int bucket = (int)(d * 5); // Scale to 0-4 range
if (bucket < 5) {
distribution[bucket]++;
}
}
Console.WriteLine("Distribution of 1000 random numbers:");
for (int i = 0; i < 5; i++) {
double range1 = i * 0.2;
double range2 = (i + 1) * 0.2;
Console.WriteLine("Range " + range1 + "-" + range2 + ": " + distribution[i] + " numbers");
}
}
}
The output of the above code is −
Distribution of 1000 random numbers: Range 0-0.2: 198 numbers Range 0.2-0.4: 205 numbers Range 0.4-0.6: 201 numbers Range 0.6-0.8: 196 numbers Range 0.8-1: 200 numbers
Common Use Cases
| Use Case | Formula | Description |
|---|---|---|
| Probability (0-1) | NextDouble() | Direct usage for probability calculations |
| Percentage (0-100) | NextDouble() * 100 | Scale to percentage range |
| Custom range (min-max) | NextDouble() * (max - min) + min | Scale to any numeric range |
Conclusion
The Random.NextDouble() method generates floating-point numbers between 0.0 (inclusive) and 1.0 (exclusive). It's particularly useful for creating probabilities, percentages, and scaling random values to custom ranges using simple mathematical operations.
