Reputation: 406
I am trying to randomize a set of questions I have in my app every time someone runs it. Right now I have
-(void)Category1{
int QuestionSelected = rand() % 100;
switch (QuestionSelected) {
case 0:
QuestionText.text = [NSString stringWithFormat:@"Here is where my first question is"];
[Answer1 setTitle:@"First multiple choice" forState:UIControlStateNormal];
[Answer2 setTitle:@"Second multiple choice" forState:UIControlStateNormal];
[Answer3 setTitle:@"Third multiple choice" forState:UIControlStateNormal];
[Answer4 setTitle:@"Fourth multiple choice" forState:UIControlStateNormal];
Answer1Correct = YES;
CorrectAnswerDisplay.text = [NSString stringWithFormat:@"Right Answer"];
break;
case 1:
QuestionText.text = [NSString stringWithFormat:@"Here is where my second question is"];
[Answer1 setTitle:@"First multiple choice" forState:UIControlStateNormal];
[Answer2 setTitle:@"Second multiple choice" forState:UIControlStateNormal];
[Answer3 setTitle:@"Third multiple choice" forState:UIControlStateNormal];
[Answer4 setTitle:@"Fourth multiple choice" forState:UIControlStateNormal];
Answer1Correct = YES;
CorrectAnswerDisplay.text = [NSString stringWithFormat:@"Right Answer"];
break;
This works all fine and dandy, but it only randomizes it once. So It randomizes all 100 questions I have in it, but if I close my app and run it again from the beginning, it randomizes the same way it did before so it runs exactly the same as it did before with the same questions. Is there something better than my rand() line?
Upvotes: 0
Views: 127
Reputation: 15015
Replace this line:-
int QuestionSelected = rand() % 100;
With modified below line:-
int QuestionSelected=(int)
arc4random_uniform(100)
Upvotes: 0
Reputation: 318824
rand
needs to be seeded with a call to srand
. Better yet, replace your use of rand
with arc4random
or arc4random_uniform
. It doesn't need to be seeded and gives better results.
Upvotes: 1