繁体   English   中英

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

[英]Randomize questions every time an app loads in Quiz

每当有人运行我的应用程序时,我都会尝试随机分配一组问题。 现在我有

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

rand需要发出呼吁srand种子。 更好的是,将您对rand的使用替换为arc4randomarc4random_uniform 它不需要种子,可以提供更好的结果。

替换此行:-

    int QuestionSelected = rand() % 100;

修改如下:

   int QuestionSelected=(int)
   arc4random_uniform(100)

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM