I am trying to generat a random number.
Code:
int pickRandomNumbers(){
Random pickNum = new Random(); int random1 = pickNum.Next(1, 20); return random1;}
void displayQuestion()
{
num1 = pickRandomNumbers();
num2 = pickRandomNumbers();
lblQuestion.Text = "How much is " + num1 + " times " + num2 + " ?";
}
When I run the program, the question displays the same number, BUT when I Debug and follow the code, it gives me different numbers?
I understand that random uses the Time to help pick. Do i need to force it to wait a second, or what?
HELP?
AlanPosted Mar 27, 2008, 3:27 PM
Try removing this line from the pickRandomNumbers() method:
Random pickNum = new Random();
and make it into a private static field instead:
private static Random pickNum = new Random();