作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
每次有人运行我的应用程序时,我都试图将我的应用程序中的一组问题随机化。现在我有
-(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
的使用替换为 arc4random
或 arc4random_uniform
。它不需要播种并且可以提供更好的结果。
关于ios - 每次在测验中加载应用程序时随机化问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21503121/
我是一名优秀的程序员,十分优秀!