True Random Xcode

2020-05-07 06:23发布

问题:

I'm currently making a quiz app. When a user starts the quiz random questions show up like you would expect from a quiz app. The problem is, it is not quite random. It does show random questions, but the questions repeat. I wanted to make sure they do not repeat until the end! My code is :

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

        break;

    case 1:
        break;

(...)

Isn't there a better way to do it? A way to just not repeat the questions? Thank you so much!

回答1:

A shuffle may be your best solution:

// 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

ADDENDUM

Example with actual text being printed after shuffling

// 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


回答2:

The idea is to use each question once until all questions have been used.

Sample code. Note that the questionIndex does not repeat.

// 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);
}

NSLog output:
arrayIndex: 9, questionIndex: 9
arrayIndex: 5, questionIndex: 5
arrayIndex: 5, questionIndex: 6
arrayIndex: 3, questionIndex: 3
arrayIndex: 3, questionIndex: 4
arrayIndex: 4, questionIndex: 8
arrayIndex: 2, questionIndex: 2
arrayIndex: 0, questionIndex: 0
arrayIndex: 1, questionIndex: 7
arrayIndex: 0, questionIndex: 1



回答3:

Any random generator is actually pseudorandom. By default it is started from the same initial value. To make it it "real random" you should supply unique start value i.e. "salt" for each run. As a simplest approach you can use [NSDate timeIntervalSinceReferenceDate].



回答4:

Put your questions in an array and put the random number in the objectWithIndex method of NSMutableArray. Then remove the question from the array. Whenever a index is chosen, but there is not a question anymore, try it again.