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 Numbers in C#
The Random class in C# is used to generate pseudo-random numbers. It provides various methods to generate random integers, doubles, and bytes within specified ranges.
Syntax
Following is the syntax for creating a Random object and generating random numbers −
Random rd = new Random(); int randomNumber = rd.Next(minValue, maxValue);
Following are the commonly used methods of the Random class −
rd.Next() // Random int from 0 to int.MaxValue rd.Next(maxValue) // Random int from 0 to maxValue-1 rd.Next(minValue, maxValue) // Random int from minValue to maxValue-1 rd.NextDouble() // Random double from 0.0 to 1.0
Parameters
minValue − The inclusive lower bound of the random number.
maxValue − The exclusive upper bound of the random number (the generated number will be less than this value).
Return Value
The Next() method returns a random integer within the specified range. The NextDouble() method returns a random floating-point number between 0.0 and 1.0.
Using Next() with Different Parameters
Example
using System;
class Program {
static void Main() {
Random rd = new Random();
// Random number between 100 and 199
int rand_num1 = rd.Next(100, 200);
Console.WriteLine("Random number (100-199): " + rand_num1);
// Random number between 0 and 99
int rand_num2 = rd.Next(100);
Console.WriteLine("Random number (0-99): " + rand_num2);
// Random number (full range)
int rand_num3 = rd.Next();
Console.WriteLine("Random number (full range): " + rand_num3);
}
}
The output of the above code is −
Random number (100-199): 147 Random number (0-99): 73 Random number (full range): 1891234567
Using NextDouble() for Decimal Numbers
Example
using System;
class Program {
static void Main() {
Random rd = new Random();
// Random double between 0.0 and 1.0
double rand_double = rd.NextDouble();
Console.WriteLine("Random double (0-1): " + rand_double);
// Random double in custom range (10.0 to 50.0)
double custom_range = rd.NextDouble() * (50.0 - 10.0) + 10.0;
Console.WriteLine("Random double (10-50): " + custom_range);
}
}
The output of the above code is −
Random double (0-1): 0.7234567891234567 Random double (10-50): 38.94567123456789
Generating Multiple Random Numbers
Example
using System;
class Program {
static void Main() {
Random rd = new Random();
Console.WriteLine("Five random numbers between 1 and 10:");
for (int i = 0; i < 5; i++) {
int randomNum = rd.Next(1, 11);
Console.WriteLine("Number " + (i + 1) + ": " + randomNum);
}
}
}
The output of the above code is −
Five random numbers between 1 and 10: Number 1: 7 Number 2: 3 Number 3: 9 Number 4: 1 Number 5: 6
Key Points
The
maxValueparameter is exclusive, meaning the generated number will be less than this value.For better randomness in applications, consider using
Random(seed)with a time-based seed.The same Random instance should be reused to avoid generating identical sequences when created in quick succession.
Conclusion
The Random class in C# provides an easy way to generate pseudo-random numbers using methods like Next() for integers and NextDouble() for decimal values. Remember that the upper bound in Next(min, max) is exclusive, so the actual range is from min to max-1.
