Generate Random Numbers in Range

Use the Next(int) method overload to generate a random integer that is less than the specified maximum value.

The following example generates the positive random numbers that are less than 10.

Random random = new Random();

for (int i = 0; i < 4; i++)
{
    Console.WriteLine(random.Next(10)); // returns random integers < 10
}

Comments