Generate Random Numbers in C#


Here you will learn how to generate random numbers in C#.

C# provides the Random class to generate random numbers based on the seed value. Use the following methods of the Random class to generate random numbers.

Method Description
Next() Returns a positive random integer within the default range -2,147,483,648 to 2,147,483, 647.
Next(int) Returns a positive random integer that is less than the specified maximum value.
Next(int, int) Returns a positive random integer within the specified minimum and maximum range (includes min and excludes max).
NextDouble() Generates random floating-point number that is greater than or equal to 0.0 and less than 1.0.
NextByte() Fills the specified array with the random bytes.

The following example demonstrates how to generate a random integers.

Example: Generate Random Integers
Random rnd = new Random();
int num = rnd.Next();

Call the Next() method multiple times to get the multiple random numbers, as shown below.

Example: Generate Multiple Random Integers
Random rnd = new Random();

for (int j = 0; j < 4; j++)
{
    Console.WriteLine(rnd.Next());
}
Output:
1342905725
1212164411
1300310820
1963559478

Generate Random Numbers in Range

Use the Next(int) method overload to generate a random integer that is less than the specified maximum value.

The following example generates the positive random numbers that are less than 10.

Example: Generate Random Integers
Random rnd = new Random();

for (int j = 0; j < 4; j++)
{
    Console.WriteLine(rnd.Next(10));//returns random integers < 10
}
Output:
0
6
5
2

Generate Random Number in Min to Max Range

Use the Next(int min, int max) overload method to get a random integer that is within a specified range.

Example: Generate Random Integers in Range
Random rnd = new Random();

for(int j = 0; j < 4; j++)
{
    Console.WriteLine(rnd.Next(10, 20)); // returns random integers >= 10 and < 20
}
Output:
13
18
10
15

In the above example, rnd.Next(10, 20) generate random numbers that will be between 10 to 19.

Generate Random Floating Point Number

Use the NextDouble() method to get a random floating-point number between 0.0 to 1.0, as shown below.

Example: Generate Random Floats
Random rnd = new Random();

for(int j = 0; j < 4; j++)
{    
    Console.WriteLine(rnd.NextDouble());
}
Output:
0.950496411859289
0.751511535491567
0.757902594170488
0.692590497290991

Generate Random Bytes

Use the NextBytes() method to generate a series of byte values. Pass an array to fill the number of byte values.

The following example shows how to generate a random number using NextBytes() method:

Example: Generate Random Bytes
Random rnd = new Random();
byte[] randomBytes = new byte[4];

rnd.NextBytes(randomBytes);

foreach (byte val in randomBytes)
{ 
    Console.WriteLine(val);
}
Output:
36
144
140
231

Seed Value

The Random class uses the seed value as a starting value for the pseudo-random number generation algorithm. By default, the Random class uses the system clock to generate its seed value so that each instance of the Random class can generate different random numbers.

Two different instances of the Random class having the same seed value will generate the same random numbers, as shown below.

Example: Seed Value in Random Class
Random rnd1 = new Random(10); //seed value 10
for (int j = 0; j < 4; j++){
    Console.WriteLine(rnd1.Next());
}

Console.WriteLine("Using another object");

Random rnd2 = new Random(10);//seed value 10
for (int j = 0; j < 4; j++){
    Console.WriteLine(rnd2.Next());
}
Output:
2041175501
1613858733
Using another object
2041175501
1613858733

In the above example, two different objects of the Random class are instantiated with the same seed value. So, they will generate the same random numbers.

By default, the seed value is time-dependent. In .NET Core, the default seed value is produced by the thread-static, pseudo-random number generator.