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.Next() Method in C#
The Random.Next() method in C# is used to generate non-negative random integers. It provides multiple overloads to control the range of generated numbers, making it useful for various scenarios like games, simulations, and testing.
Syntax
The Random.Next() method has three overloads −
public virtual int Next(); public virtual int Next(int maxValue); public virtual int Next(int minValue, int maxValue);
Parameters
maxValue − The exclusive upper bound of the random number (0 to maxValue-1).
minValue − The inclusive lower bound of the random number.
maxValue − The exclusive upper bound of the random number (minValue to maxValue-1).
Return Value
Returns a 32-bit signed integer greater than or equal to 0 and less than Int32.MaxValue for the parameterless version, or within the specified range for other overloads.
Using Random.Next() Without Parameters
This overload generates random integers from 0 to Int32.MaxValue −
using System;
public class Demo {
public static void Main() {
Random r = new Random();
Console.WriteLine("Random numbers.....");
for (int i = 1; i <= 5; i++)
Console.WriteLine(r.Next());
}
}
The output of the above code is −
Random numbers..... 1014639030 1510161246 1783253715 487417801 249480649
Using Random.Next() with Maximum Value
This overload generates random integers from 0 to the specified maximum value (exclusive) −
using System;
public class Demo {
public static void Main() {
Random r = new Random();
Console.WriteLine("Random numbers from 0 to 9.....");
for (int i = 1; i <= 5; i++)
Console.WriteLine(r.Next(10));
Console.WriteLine("\nRandom numbers from 0 to 99.....");
for (int i = 1; i <= 3; i++)
Console.WriteLine(r.Next(100));
}
}
The output of the above code is −
Random numbers from 0 to 9..... 2 7 8 5 9 Random numbers from 0 to 99..... 43 76 21
Using Random.Next() with Range
This overload generates random integers within a specified range using minimum and maximum values −
using System;
public class Demo {
public static void Main() {
Random r = new Random();
Console.WriteLine("Random numbers from 10 to 20.....");
for (int i = 1; i <= 5; i++)
Console.WriteLine(r.Next(10, 21));
Console.WriteLine("\nRandom numbers from 50 to 59.....");
for (int i = 1; i <= 3; i++)
Console.WriteLine(r.Next(50, 60));
}
}
The output of the above code is −
Random numbers from 10 to 20..... 15 18 12 20 11 Random numbers from 50 to 59..... 54 57 52
Common Use Cases
| Use Case | Method | Example |
|---|---|---|
| Dice roll (1-6) | Next(1, 7) | r.Next(1, 7) |
| Array index (0 to length-1) | Next(array.Length) | r.Next(arr.Length) |
| Percentage (0-99) | Next(100) | r.Next(100) |
| Large random number | Next() | r.Next() |
Conclusion
The Random.Next() method provides flexible random integer generation with three overloads for different scenarios. Use the parameterless version for large ranges, specify a maximum for bounded ranges, or use both minimum and maximum parameters for custom ranges where the maximum value is always exclusive.
