In this article, we are going to be learning how to Generate Random Numbers in C#.
So, let’s start.
Generate Random Numbers
There are two main classes that exist in C# to create random numbers: Random
and RandomNumberGenerator
.
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!
Thank you for sharing. This was very helpful!
Thank you to Kevin. We’re glad to hear that.
Well-defined information. I want to say thank you for the information. It is such useful info to all of us.
Thank you, Payton.
Good to know, in .NET 6.0 we finally got a Random.Shared static property giving a completely thread safe implementation.
Calling new Random().Next() in a loop will give the same result (since it’s based on time), so you would have to keep the Random instance, but calling it from multiple threads (for example having a static readonly Random instance in a Web app) has the nasty side effect that it will get stuck in returning 0 every time you call it.
Random.Shared.Next() can now be called from any code, concurrently, with always have pseudo random results.
Hello Steve. Thanks a lot for the info you’ve provided. It is great to know that as well.