In this blog post, we will get a random number between 1 and 100 using C#.

We will use the Random class that C# provides us. We will instantiate the Random class and then provide a range to the .Next() method.

 

 

Random number between 1 and 100

// Instantiate the random class var random = new Random(); // Provide a range to the Next method random.Next(1, 100); // Result: // Returns a random number between 1 and 99 (both inclusive)

Note: This method returns a random number between the range provided. Random number can be greater than or equal to the minimum number provided and less than the max number.

 

 

Get A Random Number C#(No Range Provided)

In C#, we can use the Random.Next() method and provide no values to the range. When no values are provided, the method uses 0 as the minimum value and Int.MaxValue as the max range.

// Instantiate the random class var random = new Random(); // No range to the Next method random.Next(); // Result: // Returns a random number between 0 and (Int.MaxValue - 1) (both inclusive)