I'm writing a a multiple-choice quiz program for Android, and would like to add a randomization feature to it, that is, the questions will come from a list, and the order will differ each time a quiz is taken. I have a list of about 600 questions which are sequentially numbered and stored in a hash map. The limitation is that I don't want to ask the same question twice during a given quiz.
I was initially thinking about creating a separate map to track the questions asked. Then, to get the next question, have a loop which gets a random number (based on the range of questions the user has specified to select from), and check the answered map to see if that question has already been asked. However, as the number of questions answered increases, this would require a linearly increasing amount of lookups for each question asked, until 100 lookups is reached.
If I limit the number of questions on a given quiz to 100, then 100 lookups might not take a long time. I could put a small delay factor in to keep the user for noticing.
Another possible solution is to create a map containing an the unasked questions at the start of the quiz. When a question is answered, delete it from the unasked question map. To select the next question, convert the unanswered question map to an array, get the array size, and get a random number between zero and the array size. Use that entry from the unanswered question array.
This second solution appeals due to its simplicity, but the array extraction process could get slow with a large number of questions to select from (it could eventually number in the thousands).
What is a good way to do this?
Create an array with the integers from 1 to 600, shuffle it and take the first N elements for your questions. That will give you a random chain of numbers without repetitions.
This might help: http://en.wikipedia.org/wiki/Fisher%E2%80%93Yates_shuffle
It is an algorithm to shuffle the items in a list. The question I have is: will one quiz consist of all 600 questions, or does the user get asked a random subset of the questions and if so, is it possible for them to get asked different questions from the 600 again later? You could always create and save a user-specific shuffled ordering for each user so that they can resume their shuffled quiz later...
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With