I’m trying to build a simple guessing game. I’m having a weird problem:
-(int)setRandom {
randomNum=(int)arc4random() % 100;
return randomNum;
}
-(IBAction)submit
{
num=[self setRandom];
if([numberField.text intValue] > num)
randomNumLabel.text=@"Too high. Try again";
else if([numberField.text intValue] < num)
randomNumLabel.text=@"Too low. Try again";
else
randomNumLabel.text=@"You got it, congrats!";
}
The problem is that I get a new random number every time I press submit. I thought that the first method would create the random number, and it would be the same every time, but apparently not. How do I fix this?
For this purpose you need a global variable.so first a fall declare
NSInteger num; in .h class then in
viewDidLoadwrite like thisthen in submit
Here you get a random number at view load then you get new random number when your answer matches with the number.
So this will help you.