Ios 真随机Xcode

Ios 真随机Xcode,ios,iphone,xcode,random,arc4random,Ios,Iphone,Xcode,Random,Arc4random,我目前正在制作一个测验应用程序。当用户启动测验时,随机问题会出现,就像你在测验应用程序中预期的那样。问题是,这不是完全随机的。它确实显示随机问题,但问题重复。我想确保他们不会重复到最后!我的代码是: int Questions = arc4random_uniform(142); switch (Questions) { case 0: break; case 1: break; (...) 难道没有更好的方法吗?一种不重复问题的方法?非常

我目前正在制作一个测验应用程序。当用户启动测验时,随机问题会出现,就像你在测验应用程序中预期的那样。问题是,这不是完全随机的。它确实显示随机问题,但问题重复。我想确保他们不会重复到最后!我的代码是:

int Questions = arc4random_uniform(142);
switch (Questions) {
    case 0:

        break;

    case 1:
        break;

(...)

难道没有更好的方法吗?一种不重复问题的方法?非常感谢你

任何随机生成器实际上都是伪随机的。默认情况下,它从相同的初始值开始。要使其“真正随机”,您应该为每次跑步提供唯一的开始值,即“salt”。作为一种最简单的方法,您可以使用[NSDate timeIntervalSinceReferenceDate]。

任何随机生成器实际上都是伪随机的。默认情况下,它从相同的初始值开始。要使其“真正随机”,您应该为每次跑步提供唯一的开始值,即“salt”。作为一种最简单的方法,您可以使用[NSDate timeIntervalSinceReferenceDate]。

任何随机生成器实际上都是伪随机的。默认情况下,它从相同的初始值开始。要使其“真正随机”,您应该为每次跑步提供唯一的开始值,即“salt”。作为一种最简单的方法,您可以使用[NSDate timeIntervalSinceReferenceDate]。

任何随机生成器实际上都是伪随机的。默认情况下,它从相同的初始值开始。要使其“真正随机”,您应该为每次跑步提供唯一的开始值,即“salt”。作为一种最简单的方法,您可以使用[NSDate timeIntervalSinceReferenceDate]。

将您的问题放入数组中,并将随机数放入
NSMUTABLEARRY
objectWithIndex
方法中。然后从数组中删除该问题。无论何时选择索引,但不再有问题,请重试。

将问题放入数组中,并将随机数放入
NSMutableArray
objectWithIndex
方法中。然后从数组中删除该问题。无论何时选择索引,但不再有问题,请重试。

将问题放入数组中,并将随机数放入
NSMutableArray
objectWithIndex
方法中。然后从数组中删除该问题。无论何时选择索引,但不再有问题,请重试。

将问题放入数组中,并将随机数放入
NSMutableArray
objectWithIndex
方法中。然后从数组中删除该问题。每当选择了索引,但不再有问题时,请再试一次。

方法是对每个问题使用一次,直到所有问题都使用完毕

示例代码。请注意,questionIndex不会重复

// Setup
int questionCount = 10; // real number of questions
NSMutableArray *questionIndexes = [NSMutableArray array];
for (int i=0; i<questionCount; i++)
    [questionIndexes addObject:@(i)];

// Simulate asking all questions
while (questionIndexes.count) {
    // For each round 
    unsigned long arrayIndex = arc4random_uniform((uint32_t)questionIndexes.count);
    int questionIndex = [questionIndexes[arrayIndex] intValue];
    [questionIndexes removeObjectAtIndex:arrayIndex];
    NSLog(@"arrayIndex: %lu, questionIndex: %i", arrayIndex, questionIndex);
}
//设置
int questionCount=10;//实际问题数
NSMutableArray*questionIndexes=[NSMutableArray];

对于(int i=0;i来说,我们的想法是在所有问题都被使用之前,每个问题只使用一次

示例代码。请注意,questionIndex没有重复

// Setup
int questionCount = 10; // real number of questions
NSMutableArray *questionIndexes = [NSMutableArray array];
for (int i=0; i<questionCount; i++)
    [questionIndexes addObject:@(i)];

// Simulate asking all questions
while (questionIndexes.count) {
    // For each round 
    unsigned long arrayIndex = arc4random_uniform((uint32_t)questionIndexes.count);
    int questionIndex = [questionIndexes[arrayIndex] intValue];
    [questionIndexes removeObjectAtIndex:arrayIndex];
    NSLog(@"arrayIndex: %lu, questionIndex: %i", arrayIndex, questionIndex);
}
//设置
int questionCount=10;//问题的实际数量
NSMutableArray*questionIndexes=[NSMutableArray];

对于(int i=0;i来说,我们的想法是在所有问题都被使用之前,每个问题只使用一次

示例代码。请注意,questionIndex没有重复

// Setup
int questionCount = 10; // real number of questions
NSMutableArray *questionIndexes = [NSMutableArray array];
for (int i=0; i<questionCount; i++)
    [questionIndexes addObject:@(i)];

// Simulate asking all questions
while (questionIndexes.count) {
    // For each round 
    unsigned long arrayIndex = arc4random_uniform((uint32_t)questionIndexes.count);
    int questionIndex = [questionIndexes[arrayIndex] intValue];
    [questionIndexes removeObjectAtIndex:arrayIndex];
    NSLog(@"arrayIndex: %lu, questionIndex: %i", arrayIndex, questionIndex);
}
//设置
int questionCount=10;//问题的实际数量
NSMutableArray*questionIndexes=[NSMutableArray];

对于(int i=0;i来说,我们的想法是在所有问题都被使用之前,每个问题只使用一次

示例代码。请注意,questionIndex没有重复

// Setup
int questionCount = 10; // real number of questions
NSMutableArray *questionIndexes = [NSMutableArray array];
for (int i=0; i<questionCount; i++)
    [questionIndexes addObject:@(i)];

// Simulate asking all questions
while (questionIndexes.count) {
    // For each round 
    unsigned long arrayIndex = arc4random_uniform((uint32_t)questionIndexes.count);
    int questionIndex = [questionIndexes[arrayIndex] intValue];
    [questionIndexes removeObjectAtIndex:arrayIndex];
    NSLog(@"arrayIndex: %lu, questionIndex: %i", arrayIndex, questionIndex);
}
//设置
int questionCount=10;//问题的实际数量
NSMutableArray*questionIndexes=[NSMutableArray];
对于(int i=0;iA)可能是您的最佳解决方案:

// Setup
int questionCount = 10; // real number of questions
NSMutableArray *questionIndices = [NSMutableArray array];
for (int i = 0; i < questionCount; i++) {
    [questionIndices addObject:@(i)];
}
// shuffle
for (int i = questionCount - 1; i > 0; --i) {
    [questionIndices exchangeObjectAtIndex: i
        withObjectAtIndex: arc4random_uniform((uint32_t)i + 1)];
}
// Simulate asking all questions
for (int i = 0; i < questionCount; i++) {
    NSLog(@"questionIndex: %i", [questionIndices[i] intValue]);
}


NSLog output:
questionIndex: 6
questionIndex: 2
questionIndex: 4
questionIndex: 8
questionIndex: 3
questionIndex: 0
questionIndex: 1
questionIndex: 9
questionIndex: 7
questionIndex: 5
//设置
int questionCount=10;//问题的实际数量
NSMutableArray*问题索引=[NSMutableArray];
for(int i=0;i0;--i){
[问题索引交换对象索引:i
withObjectAtIndex:arc4random_uniform((uint32_t)i+1)];
}
//模拟询问所有问题
for(int i=0;i
附录

洗牌后打印实际文本的示例

// Setup
NSMutableArray *question = [NSMutableArray arrayWithObjects:
    @"Q0 text", @"Q1 text", @"Q2 text", @"Q3 text", @"Q4 text",
    @"Q5 text", @"Q6 text", @"Q7 text", @"Q8 text", @"Q9 text", nil];
// shuffle
for (int i = (int)[question count] - 1; i > 0; --i) {
    [question exchangeObjectAtIndex: i
        withObjectAtIndex: arc4random_uniform((uint32_t)i + 1)];
}
// Simulate asking all questions
for (int i = 0; i < [question count]; i++) {
    printf("%s\n", [question[i] UTF8String]);
}

Sample output:
Q9 text
Q5 text
Q6 text
Q4 text
Q1 text
Q8 text
Q3 text
Q0 text
Q7 text
Q2 text
//设置
NSMUTABLEARRY*问题=[NSMUTABLEARRY arrayWithObjects:
@“Q0文本”、@“Q1文本”、@“Q2文本”、@“Q3文本”、@“Q4文本”,
@"Q5文本,"Q6文本,"Q7文本,"Q8文本,"Q9文本,无";;
//洗牌
对于(int i=(int)[问题计数]-1;i>0;--i){
[问题交换对象索引:i
withObjectAtIndex:arc4random_uniform((uint32_t)i+1)];
}
//模拟询问所有问题
对于(int i=0;i<[问题计数];i++){
printf(“%s\n”,[question[i]UTF8String]);
}
样本输出:
问题9文本
Q5文本
Q6文本
第四季度文本
Q1文本
Q8文本
Q3文本
Q0文本
Q7文本
Q2文本
A可能是您的最佳解决方案:

// Setup
int questionCount = 10; // real number of questions
NSMutableArray *questionIndices = [NSMutableArray array];
for (int i = 0; i < questionCount; i++) {
    [questionIndices addObject:@(i)];
}
// shuffle
for (int i = questionCount - 1; i > 0; --i) {
    [questionIndices exchangeObjectAtIndex: i
        withObjectAtIndex: arc4random_uniform((uint32_t)i + 1)];
}
// Simulate asking all questions
for (int i = 0; i < questionCount; i++) {
    NSLog(@"questionIndex: %i", [questionIndices[i] intValue]);
}


NSLog output:
questionIndex: 6
questionIndex: 2
questionIndex: 4
questionIndex: 8
questionIndex: 3
questionIndex: 0
questionIndex: 1
questionIndex: 9
questionIndex: 7
questionIndex: 5
//设置
int questionCount=10;//问题的实际数量
NSMutableArray*问题索引=[NSMutableArray];
for(int i=0;i0;--i){
[问题索引交换对象索引:i
withObjectAtIndex:arc4random_uniform((uint32_t)i+1)];
}
//模拟询问所有问题
for(int i=0;i
附录

洗牌后打印实际文本的示例

// Setup
NSMutableArray *question = [NSMutableArray arrayWithObjects:
    @"Q0 text", @"Q1 text", @"Q2 text", @"Q3 text", @"Q4 text",
    @"Q5 text", @"Q6 text", @"Q7 text", @"Q8 text", @"Q9 text", nil];
// shuffle
for (int i = (int)[question count] - 1; i > 0; --i) {
    [question exchangeObjectAtIndex: i
        withObjectAtIndex: arc4random_uniform((uint32_t)i + 1)];
}
// Simulate asking all questions
for (int i = 0; i < [question count]; i++) {
    printf("%s\n", [question[i] UTF8String]);
}

Sample output:
Q9 text
Q5 text
Q6 text
Q4 text
Q1 text
Q8 text
Q3 text
Q0 text
Q7 text
Q2 text
//设置
NSMUTABLEARRY*问题=[NSMutable