Ios 如何将从词典plist中提取的问题随机排列

Ios 如何将从词典plist中提取的问题随机排列,ios,dictionary,plist,Ios,Dictionary,Plist,嘿,伙计们,对我从我的-plist文件中提取的问题进行随机分组有什么想法吗 -(NSUInteger)nextQuestionID:(NSUInteger)question_number{ return (question_number>=[self.questions count]-1) ? 0 : (question_number+1); return 0; } -(NSDictionary*) getQuestion:(NSUInteger)question_number{ if

嘿,伙计们,对我从我的-plist文件中提取的问题进行随机分组有什么想法吗

-(NSUInteger)nextQuestionID:(NSUInteger)question_number{
return (question_number>=[self.questions count]-1) ? 0 : (question_number+1);
return 0;

}

-(NSDictionary*) getQuestion:(NSUInteger)question_number{
if (question_number>=[self.questions count]) question_number = 0;
return [self.questions objectAtIndex:question_number];
return NULL;


}

要获得一个随机整数,我建议使用
arc4random
函数,下面是一些代码:

int randomInt = arc4random() % questionNumber;
return [self.questions objectAtIndex: randomInt];

question\u number=arc4random()%self.questions.count

将问题编号设置为随机数

替换该函数

-(NSDictionary*) getQuestion:(NSUInteger)question_number{
   if (question_number>=[self.questions count]) question_number = 0;
   question_number = arc4random()%[self.questions count];// changes made here
   return [self.questions objectAtIndex:question_number];
   return NULL;


}

您可以使用
arc4random\u uniform(上限)
功能。该参数是随机数的上限

例如:

int index = arc4random_uniform([self.question count]);
随机整数:

srand(time(nil));
for (int i = 0; i < 100; i++) {
    NSLog(@"random number between 72 and 96 : %d", rand() % (96 - 72) + 72);
}
更新#2

测试以下回路两次、三次或更多次,并比较输出:

srand(time(nil));
for (int i = 0; i < 10; i++) {
    NSLog(@"random number : %d", rand() % 89 + 10);
}
srand(时间(nil));
对于(int i=0;i<10;i++){
NSLog(@“随机数:%d”,rand()%89+10);
}
你应该得到10个介于10和99之间的随机数,我已经在真实设备上测试过了,不是在模拟器上,但它也应该在模拟器上工作


如果您总是得到相同的结果,请告诉我。

来自
man arc4random
-
[arc4random]。。。建议使用“arc4random()%upper_bound”这样的结构,因为它避免了上界不是二的幂时的“模偏差”。
Hello holex,我们已经按照您的建议输入了代码,但似乎没有拉出随机问题,下面是我们输入的内容-(NSUInteger)nextQuestionID:(NSUInteger)问题编号{srand(time(nil));返回rand()%[self.questions count];}-(NSDictionary*)getQuestion:(NSUInteger)问题编号{if(question\u number>=[self.questions count])问题编号=0;返回[self questions.questions对象索引:问题编号];返回NULL;}谢谢你的帮助Holex,我仍然在努力让它工作,我已经用上面的代码替换了我的代码,但是仍然没有运气,我是否遗漏了使它正常工作所需要的任何东西?很抱歉问了这么多问题,我对这一切都不熟悉。谢谢你,你说你挣扎是什么意思?它从数据库中返回随机问卷,即我的上一次测试给出了400个问卷:
29418518232137325162232243521882383253962392392702355471911361072751703381798184166 102 17 68 83200 25 2828282833132 4 59 1892828328303555,273、327、274、266、121、278、346、130、71、369、353、49、355、157、339、301、139、47、40、316、239、11、212、100、220、199、80、152、208、86、133、249
。。。对我来说这看起来很随意。是什么让问题依然存在?我的代码只是以相同的顺序给了我相同的问题,我不知道为什么或者我能做些什么。我假设我丢失了一段代码,或者这里有什么东西阻止它工作。再次感谢您的帮助。请按照我在更新答案的更新2部分中提到的内容进行测试。谢谢阿里的帮助,我已经试过把它放进去,但它似乎不起作用,我现在这样放,我猜是错误的-(NSUInteger)nextQuestionID:(NSUInteger)问题编号{返回(问题编号>=[self.questions count]-1)?0:(问题编号+1);返回0;}-(NSDictionary*)getQuestion:(NSInteger)问题编号{if(问题编号>=arc4random()%[self.questions count])问题编号=arc4random();返回[self.questions objectAtIndex:问题编号];返回NULL;}你知道你在写什么吗?基本上你只是写了“生成一个随机数,然后如果
question\u number
大于或等于这个随机数,然后再生成另一个随机数”。你认为这段代码会做什么?
srand(time(nil));
for (int i = 0; i < 10; i++) {
    NSLog(@"random number : %d", rand() % 89 + 10);
}