C# – Get a Random Number Between x and y
Friday, October 31st, 2008Here’s a quick function for getting random numbers in C#:
Random rnd = new Random();
protected int GetRandomInt(int min, int max)
{
return rnd.Next(min,max);
}
int myInt = GetRandomInt(5, 1000); //gives in random integer between 5 & 1000
Notice that you have to declare your instance of the Random class outside of the GetRandomInt function if you are going to be running this in a loop.
Popularity: 28%



