ChipperT
Banned in 13 Countries
- Local time
- Today, 01:24
- Joined
- Jun 1, 2010
- Messages
- 347
I am creating a quiz program from young people based on academic Knowledge Bowl rules. Currently the "universe" of questions numbers just under 3,000 and are stored in an Access 2007 database. From this set of questions, each match will consist of x number of questions (standard is 30 but that is variable depending on the options that the quizmaster controls). Each question has an "index" to identify that question. I was using the following code to insure that no question repeated in a match:
rsCount = rs.RecordCount
numQuestions = 30 'this number can change depending on the quiz set up options
redim arrQuestionIndex(numQuestions)
Randomize
for i = 0 to numQuestions
rI = 1 + Int((rsCount) - 1) * Rnd())
for j = 0 to ubound(arrQuestionIndex) -1
if rI=arrQuestionIndex(j) Then
i=i-1
Exit For
End If
Next
arrQuestionIndex(i) = rI
Next
That worked well and no question repeated. However, there are many matches in a tournament and I found that in subsequent matches a high percentage of the questions would be repeated from the match before. Unacceptable! I tried building an iniitial array based on the code above containing the indexes of all questions in the question set from which I would then use index + numQuestions for a match, noving the index down each time until I ran out of questions in the initial array, at which time I would re-randomize the question set and rebuild the array. The problem with that is that it would take a long, long time to build that array with that many interations through the loop!
My next idea was the have a temp array (arrTemp) of question indexes pulled from a table query with rs.GetRows. I would then randomize and select a random index as above. I would then move the question index from tempArray to arrQuestionIndex and rebuild the tempArray without the selected cell. That made the task pretty fast since each time tempArray was one smaller. But it seems to be very messy!
Just curious if anyone else has a more elegant solution for this problem.
rsCount = rs.RecordCount
numQuestions = 30 'this number can change depending on the quiz set up options
redim arrQuestionIndex(numQuestions)
Randomize
for i = 0 to numQuestions
rI = 1 + Int((rsCount) - 1) * Rnd())
for j = 0 to ubound(arrQuestionIndex) -1
if rI=arrQuestionIndex(j) Then
i=i-1
Exit For
End If
Next
arrQuestionIndex(i) = rI
Next
That worked well and no question repeated. However, there are many matches in a tournament and I found that in subsequent matches a high percentage of the questions would be repeated from the match before. Unacceptable! I tried building an iniitial array based on the code above containing the indexes of all questions in the question set from which I would then use index + numQuestions for a match, noving the index down each time until I ran out of questions in the initial array, at which time I would re-randomize the question set and rebuild the array. The problem with that is that it would take a long, long time to build that array with that many interations through the loop!
My next idea was the have a temp array (arrTemp) of question indexes pulled from a table query with rs.GetRows. I would then randomize and select a random index as above. I would then move the question index from tempArray to arrQuestionIndex and rebuild the tempArray without the selected cell. That made the task pretty fast since each time tempArray was one smaller. But it seems to be very messy!
Just curious if anyone else has a more elegant solution for this problem.