每次应用在测验中加载时随机化问题

时间:2014-02-01 20:18:38

标签: ios objective-c

我试图在每次有人运行时随机化我的应用程序中的一组问题。现在我有

-(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()更好的东西?

2 个答案:

答案 0 :(得分:1)

需要通过调用rand来播种

srand。更好的是,将rand替换为arc4randomarc4random_uniform。它不需要播种并提供更好的结果。

答案 1 :(得分:0)

替换此行: -

    int QuestionSelected = rand() % 100;

修改以下行: -

   int QuestionSelected=(int)
   arc4random_uniform(100)