In this article, we are going to be learning how to Generate Random Numbers in C#.

To download the source code for this article, you can visit our GitHub repository.

So, let’s start.

Generate Random Numbers

There are two main classes that exist in C# to create random numbers:  Random and RandomNumberGenerator.

Support Code Maze on Patreon to get rid of ads and get the best discounts on our products!
Become a patron at Patreon!

Random is a pseudorandom number generator that we can initialize using the new keyword:

var random = new Random();

We have the option of providing our own seed or allowing the computer to use the system clock time as the seed to produce the required sequence of numbers:

var seed = 3;
var random = new Random(seed);

Using the same seed for separate Random objects within the same environment will generate the same series of random numbers.

RandomNumberGenerator is a secure number generator that we can initialize using the Create method:

RandomNumberGenerator.Create();

Notice that we do not provide our own input for generating the random numbers, but that the class will securely handle creating a random number based upon hidden criteria.

How to Generate a Random Integer

Once initialized we can retrieve a random number from the Random class:

var rNum = random.Next();

This will return an integer between -1 and 2147483647.

We call the RandomNumberGenerator class in a slightly different way:

var upperBound = 200;
var rngNum = RandomNumberGenerator.GetInt32(upperBound);

The parameter specified is the exclusive upper bound — meaning that the random number will be between -1 and the specified upper bound (not including it).

How to Generate a Random Integer from a Range

To accomplish this, we can use an overload of the Random.Next method.   This method has two additional parameters that we can use to specify the upper and lower bound of the generated random number:

var lowerBound = 0;
var upperBound = 1001;
var rNum = random.Next(lowerBound, upperBound);

Keep in mind that if both upper and lower bounds are the same, the lower bound number will be returned.  Furthermore, this method does allow for the returning of random negative numbers if the lower bound is less than zero.

We can generate a secure random number with a specific number of digits by similarly using an overloaded method that specifies the upper and lower bound of the random range:

var lowerBound = 0; 
var upperBound = 20001;
var rngNum = RandomNumberGenerator.GetInt32(lowerBound,upperBound);

For both methods, the first parameter is inclusive while the second parameter is exclusive; meaning that the method could return the lower bound but could not return the upper bound.

Conclusion

In conclusion, the two main ways to generate random numbers in C# is using the Random and RandomNumberGenerator classes. These pseudo-random and secure random generators provide the flexibility to generate random numbers in C# in a way that best fits your current need. The C# language has provided excellent tools for you to quickly and efficiently generate random numbers in C#. We hope that you will be able to take this knowledge and use it effectively in the near future!