How to generate a random int number?
To generate a random int in C# we can use TheRandom
class.This class has a built in method
Next()
which will generate a new number every time it's called, it can also have 2 optional parameters to create numbers between a range.Example
using System;
public class Example
{
public static void Main()
{
Random rnd = new Random();
int number = rnd.Next();
Console.WriteLine(number);
}
}
Output
2032384056
Example of creating random number between 2 numbers
using System;
public class Example
{
public static void Main()
{
Random rnd = new Random();
int number = rnd.Next(1, 13); // This will create a random number between 1 and 13
Console.WriteLine(number);
}
}
Output
5