Reputation:
I am programming a quiz, that serves the user a question, he then has to answer.
If the user answers correct, there should be less chance for getting the same question, If the answer is wrong, there should be a bigger chance for getting the same question again, then there is for getting a normal question.
My solution has been to create 5 arrays containing the questions, and then move the questions around and serve them like this:
int rand = random.nextInt(100);
if (rand < 5) {
// Question answered correct 2 times - 5 % chance
} else if (rand > 4 && rand < 15) {
// Question answered correct 1 time - 10 % chance
} else if (rand > 14 && rand < 35) {
// Normal question - 20 % chance
} else if (rand > 34 && rand < 65) {
// Answered wrong 1 time - 30 % chance
} else if (rand > 64 && rand < 100) {
// Answered wrong 2 times - 35 % chance
}
But lets say I answer a question correct 1 or 2 times, there is too big a chance of getting that exact same question again, until there there are more questions that has been answered correct.
The way I have tried to fix this problem is to only generate random numbers from 15 to 100, until there has been answered correct to 100 questions.
It seems to me, that this could be implemented way better, but can't come up with a solution.
Any idea to how I could improve this?
Upvotes: 0
Views: 101
Reputation: 372
I guess you are holding all the correct answers in a list or something - why don't you just put an extra condition in the first two ifs regarding the size of this list.
Upvotes: 0
Reputation: 3050
I would implement it more like this:
You make a float array with one entry per question, that indicates the factor of how often the question is asked. Initially, all questions have a factor of 1. The trick is to lower the factor when an answer has been answered correctly. For instance, you could halve it.
To pick a question to ask, you sum up all factors in the array (if you have 250 questions, it'll initially sum to 250 but the sum will become different when the factors change) and take a random number between 0 and the sum. Let's say that this random number becomes 110.5. Then you compute the cumulative sum of the factors until they sum up to 110.5. This means, go over the array and sum up all factors, but when the sum becomes more than the random number you stop and take the question where the iteration was at at that point. For questions with a lower factor, you'll have a lower chance of hitting them. If all questions have a lower factor, the chance evens out again.
This way, you can arbitrarily increase or decrease the chance to get a question (for instance, halve the factor when the user gets a question right, and double it when he gets it wrong). Or you could, as in the current system, just set the factor to 0.5 when he gets it right once, 0.25 when he gets it right twice, 1.5 when he gets it wrong once, and 1.75 when he gets it wrong twice.
Upvotes: 1