Reader Level:
Articles

How do I Generate Random Number in C#?

By Mahesh Chand on October 05, 2005
This code snippet shows how to you generate a random number between a range in C#.
  • 0
  • 0
  • 307817

The following code snippet shows how to generate a random number between a range in C#, where min and max are 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 like this:

int returnValue = RandomNumber(5, 20);

Mahesh Chand

Mahesh Chand is founder of C# Corner. C# Corner founded in 1999 is a FREE member contributions based open platform for developers to solve problems, learn new technology and hang out.  Mahesh has been awar... Read more

COMMENT USING

Trending up