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 :
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
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!
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]
}
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: