Resolved Random Question Generator

Discussion in 'iOS Programming' started by newtomac123, Apr 20, 2014.

  1. newtomac123, Apr 20, 2014
    Last edited: Apr 21, 2014

    newtomac123 macrumors newbie

    Joined:
    Jan 8, 2014
    #1
    I've been working on a little question application for a friend, but I'm running into a small problem. I have about 20 or so questions, and I have a random number generator to basically generate a random question each time the user starts the quiz or selects "next question" after answering a question. But the problem is that I am getting duplicates of the questions.

    I was searching for a solution online, and I found a little information, but I was wondering if someone could help me out a bit.

    This was posted on another website :
    Code:
    // Setup
    int questionCount = 20; // 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
    etc...
    
    Which works perfectly! But I can't figure out how to make it only generate ONE number at a time (so that I could make it select that question) and not generate all the numbers at a time. Basically so it wouldn't repeat the questions over again. I've tried replacing the text inside
    Code:
    for (int i = 0; i < questionCount; i++) {
        [B]NSLog(@"questionIndex: %i", [questionIndices[i] intValue]);[/B]
    }
    
    With not much luck....
    I was wondering if anyone had a solution to my little issue?

    Thank you so much, I know this is minor issue, but it is driving me nuts!
     
  2. mds1256 macrumors regular

    Joined:
    Apr 9, 2011
    #2
    First you need to remove the loop you quoted above then create a new int variable starting at 0.

    Code:
    int questionNum = 0;
    
    Then call this code when you want to ask a question.

    Code:
    [questionIndices[questionNum] intValue];
    questionNum++;
    
    This should call each number one at a time each time you call the code above.

    Hope this helps
     
  3. newtomac123 thread starter macrumors newbie

    Joined:
    Jan 8, 2014
    #3
    Thank you so much

    It is working perfectly, no repeats.

    Code:
      // Setup
            int questionCount = 20; // 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)];
            }
    
            [questionIndices[questionNum] intValue];
            questionNum++;
            
            NSLog(@"QuestionNum = %i",questionNum);
             NSLog(@"Gamenum = %i",gamenum);
    
    I went ahead and removed the for loop. The only problem is that it counts directly in order whenever I call it

    Ex:
    QuestionNum = 0, then QuestionNum = 1 etc...

    It seems not to be shuffling correctly. Did I miss a step?

    Thank you so much again for the help.
     
  4. dejo Moderator

    dejo

    Staff Member

    Joined:
    Sep 2, 2004
    Location:
    The Centennial State
    #4
    Which for loop did you remove? You have two of them. Did you perhaps remove the for loop that does the shuffle?
     
  5. newtomac123, Apr 21, 2014
    Last edited: Apr 21, 2014

    newtomac123 thread starter macrumors newbie

    Joined:
    Jan 8, 2014
    #5
    Not that I know of

    N/m

    Got it working... Minor syntax error.

    Thank you again mds!
     

Share This Page