Learn how to Generate Random Integers in C#
Producing random integers in C# is easy and might be achieved utilizing the built-in Random class.
1. Utilizing the Random Class in C#
The Random class is a built-in class in C# that permits us to generate random integers. To make use of it, we have to create an occasion of the Random class and name the Subsequent methodology. The Subsequent methodology offers us the random integer inside a spread of 0 to the utmost worth of int32.
Right here’s a syntax for producing a random integer utilizing the Random class:
int randomNumber = random.Subsequent();
2. Producing a Random Integer Utilizing the Subsequent Methodology
The Subsequent methodology is probably the most used methodology of the Random class for producing random integers. As talked about earlier, it might generate a random integer with or with out a vary.
Right here’s an instance of producing a random integer:
class Program {
static void Major() {
Random random = new Random();
int randomNumber = random.Subsequent();
Console.WriteLine(“Random quantity: “ + randomNumber);
}
}
Right here within the above code, we have now generated a random quantity utilizing the Random class, which is a part of the System namespace.
Right here we outlined a brand new occasion for a Random class utilizing the key phrase that defines the category identify. The Subsequent() methodology offers a random integer, which is saved within the variable randomNumber. Lastly, we print the worth of randomNumber to the console utilizing Console.WriteLine().
3. Producing a Random Integer inside a Vary
Utilizing the Subsequent methodology, we are able to additionally generate a random quantity for a specified vary. For that, we should outline the 2 arguments. These two arguments ought to include the min and max values of the vary from which to generate the random quantity.
Following is the syntax of getting a random integer inside a spread of 1 and 100:
int randomNumber = random.Subsequent(1, 101);
The next instance offers us a random quantity between 1 and 100:
class Program {
static void Major() {
Random random = new Random();
int randomNumber = random.Subsequent(1,101);
Console.WriteLine(“Random quantity: “ + randomNumber);
}
}
Within the output, a random quantity is generated between 1 and 100.
4. Producing a Random Integer until Outlined Quantity
We will additionally modify the above code for producing a random integer to an outlined worth. Beneath talked about code will give us a random quantity lower than 10.
class Program {
static void Major() {
Random random = new Random();
int randomNumber = random.Subsequent(10);
Console.WriteLine(“Random quantity: “ + randomNumber);
}
}
Within the output we see a random integer that’s lower than 10.
5. Producing 10 Random Integers
Utilizing a for loop we are able to modify the above code and generate 10 random integers between 0 and 100.
class Program
{
static void Major(string[] args)
{
// Create a brand new occasion of the Random class
Random random = new Random();
// Generate and print 10 random integers between 0 and 99
Console.WriteLine(“Printing 10 random integers between 0 and 99:”);
for (int i = 0; i < 10; i++)
{
int randomNumber = random.Subsequent(100); // generates a random integer between 0 and 99
Console.WriteLine($“Random quantity {i + 1}: {randomNumber}”);
}
}
}
The above code creates a brand new occasion of the Random class, generates 10 random integers between 0 and 99, and prints them to the console. The random.Subsequent(100) methodology name generates a random integer between 0 and 99 (inclusive), which is assigned to the randomNumber variable. The loop repeats 10 occasions, every printing a special random quantity to the console.
Within the output, we are able to see 10 random integers printed on the display.
Conclusion
Random integers have completely different functions in programming. In C#, Subsequent methodology is usually used for producing random integers. This methodology can generate completely different random integers. These numbers might be generated for an outlined vary or until a selected quantity.