Sabari M
Updated date Oct 26, 2021
In this blog we will learn the details about Random class, Next(), NextBytes() and NextDouble() methods and how to generate random numbers using Random class.
• 1.4k
• 0
• 0

Random class generates random numbers in C#. The random class has methods like Random.Next(), Random.NextBytes(), and Random.NextDouble().

• Random.Next() method returns a non-negative random numbers.
• Random.NextBytes() returns an array of bytes filled with random numbers.
• Random.NextDouble() returns a random floating-point number which is between 0.0 and 1.0.

## Random.Next() :

The below example prints the 8 random numbers and without any restrictions in the numbers.

``````Random random = new Random();
// Print 8 random numbers
Console.WriteLine("Display 8 random numbers");
for (int i = 1; i <= 8; i++)
Console.WriteLine("{0} -->> {1}", i, random.Next());    // Generate & print Random Numbers

// Output

// Display 8 random numbers
// 1-- >> 1473913368
// 2-- >> 1571911790
// 3-- >> 1681307048
// 4-- >> 768882136
// 5-- >> 2049633432
// 6-- >> 425642955
// 7-- >> 1633951852
// 8-- >> 1824382833``````

If we need to generate the numbers specifically i.e., less than 5000 or between 1000 to 4000 then we can pass specific arguments to the Next() method.

Below example returns the random numbers which are less than 5000,

``````Random random = new Random();
// Print 5 random numbers less than 5000
Console.WriteLine("Display 5 random numbers (less than 5000)");
for (int i = 1; i <= 5; i++)
Console.WriteLine("{0} -->> {1}", i, random.Next(5000));    // Generate & print 5 Random Numbers (less than 5000)

// Output

// Display 5 random numbers(less than 5000)
// 1-- >> 2868
// 2-- >> 786
// 3-- >> 2869
// 4-- >> 1765
// 5-- >> 409``````

Below example returns the random numbers between 1000 to 4000,

``````Random random = new Random();
// Print random numbers between 1000 and 4000
Console.WriteLine("Display 5 random numbers (between 1000 and 4000)");
for (int i = 1; i <= 5; i++)
Console.WriteLine("{0} -->> {1}", i, random.Next(1000, 4000));    // Generate & print 5 Random Numbers (between 1000 and 4000)

// Output

// Display 5 random numbers(between 1000 and 4000)
// 1-- >> 1377
// 2-- >> 2510
// 3-- >> 2923
// 4-- >> 1257
// 5-- >> 1967``````

## Random.NextBytes() :

The below example prints the 8 random numbers in the byte array,

``````Random random = new Random();
Byte[] mybyte = new Byte[10];

random.NextBytes(mybyte);

// Print random numbers in the byte array
Console.WriteLine("Display 8 random numbers in the byte array");

for (int i = 0; i <= 8; i++)
Console.WriteLine("{0} -->> {1}", i, mybyte[i]);

// Output

// Display 8 random numbers in the byte array
// 0-- >> 29
// 1-- >> 108
// 2-- >> 29
// 3-- >> 142
// 4-- >> 42
// 5-- >> 4
// 6-- >> 131
// 7-- >> 244
// 8-- >> 109``````

## Random.NextDouble() :

The below example prints the 8 random floating-point numbers,

``````Random random = new Random();

// Print 8 random floating point numbers
Console.WriteLine("Display 8 random floating point numbers");
for (int i = 0; i <= 8; i++)
Console.WriteLine("{0} -->> {1}", i, random.NextDouble());

// Output

// Display 8 random floating point numbers
// 0-- >> 0.0759870331156939
// 1-- >> 0.970090401810636
// 2-- >> 0.188995888079049
// 3-- >> 0.806421628131727
// 4-- >> 0.985433087677431
// 5-- >> 0.0751320552430731
// 6-- >> 0.526657178777576
// 7-- >> 0.454447274307929
// 8-- >> 0.707477007856349``````

Sabari M
Software Professional, India

IT professional with 15+ years of experience in Microsoft Technologies with a strong base in Microsoft .NET (C#.Net, ASP.Net MVC, ASP.NET WEB API, Webservices,...Read More

https://www.techieclues.com/profile/alagu-mano-sabari-m