Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

newtomac123

macrumors newbie
Original poster
Jan 8, 2014
18
0
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!
 
Last edited:
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
 
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.
 
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?

Which for loop did you remove? You have two of them. Did you perhaps remove the for loop that does the shuffle?
 
Not that I know of

N/m

Got it working... Minor syntax error.

Thank you again mds!
 
Last edited:
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.