True Random Xcode

后端 未结 4 980
耶瑟儿~
耶瑟儿~ 2021-01-26 02:01

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 sho

相关标签:
4条回答
  • 2021-01-26 02:22

    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
    
    0 讨论(0)
  • 2021-01-26 02:26

    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.

    0 讨论(0)
  • 2021-01-26 02:36

    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].

    0 讨论(0)
  • 2021-01-26 02:38

    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

    0 讨论(0)
提交回复
热议问题