How do I Generate Random Number in C#?

The following code snippet shows how to generate a random number between a range in C#, where min and max are the minimum and maximum range of the new number.

private int RandomNumber(int min, int max)
    Random random = new Random();
    return random.Next(min, max);

How to use it?

Copy the above code in your class where you want to use it and call it like this.

int returnValue = RandomNumber(5, 20);

Similar Articles
Founded in 2003, Mindcracker is the authority in custom software development and innovation. We put best practices into action. We deliver solutions based on consumer and industry analysis.