我试图在每次有人运行时随机化我的应用程序中的一组问题。现在我有
-(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;
这一切都很好,花花公子,但它只是随机一次。因此,它随机化了我所有的100个问题,但是如果我关闭我的应用程序并从头开始再次运行它,它会以与之前相同的方式随机化,因此它运行与以前使用相同问题完全相同。有没有比我的rand()更好的东西?
答案 0 :(得分:1)
rand
来播种 srand
。更好的是,将rand
替换为arc4random
或arc4random_uniform
。它不需要播种并提供更好的结果。
答案 1 :(得分:0)
替换此行: -
int QuestionSelected = rand() % 100;
修改以下行: -
int QuestionSelected=(int)
arc4random_uniform(100)