Reputation: 547
I'm burning my mind and I cannot solve this problem.
I have a DB with QUESTIONS for a QUIZ.
I have 100 questions with 10 Points and 30 questions with 20 Points (and the idea is that I can have another questions with 30, 40 points)
I need to select randomly 20 questions.
But I need to select ALWAYS 15 questions with 10 points and 5 with 20 points.
and random all..
I can random all questions WITHOUT the "Always 15/5"
****
' Determines how many unique random numbers to be produced
tot_unique = 20
' Determines the highest value for any unique random number
top_number = 100
dim random_number, counter, check, unique_numbers
' When passing a varible for an array use redim
redim random_number(tot_unique)
' begin random function
randomize
' Begin a for next loop from one to the max number of unique numbers
For counter = 1 to tot_unique
' select a number between 1 and the top number value
random_number(counter) = Int(Rnd * top_number)+1
' For next loop to compare the values stored in the array to
' the new random value being assigned
for check=1 to counter-1
if random_number(check)= random_number(counter) then
' If the current value is equal to a previous value
' subject
counter=counter-1
end if
next ' Repeat loop to check values
next ' Repeat loop to assign values to the ar
Here I dont inform the questions with 10 or 20 points and the script random 20 UNIQUE numbers between 1-100.
I dont know how I can do this.. Any Idea?
Upvotes: 0
Views: 1152
Reputation: 3854
The only way I can think of is to generate two sets of random numbers: 15 for the 10-point questions, and 5 for the 20-point questions. Since the same question can't be both 10 points and 20 points, you don't have to check the two sets for overlaps; you just need some way to translate your sets of random numbers into a filter for your database. If the questions have consecutive IDs, this is simply a matter of adding the random numbers to the first ID (or rather, one less than the first ID). If the questions don't have consecutive IDs, I'd probably just add a column with consecutive numbers for the purpose of filtering.
For combining the two kinds of questions into one randomly-sorted list, all you really need is a random ordering of the numbers 1 through 20 (a permutation), which you could generate with a slight variation of the code you already have (and/or a little bit of web searching). Then, when you read your random selection of questions from the database, instead of putting them into an array in the order they're returned, re-order them according to your permutation. For example, if your random ordering goes (5,18,11,12,4,...)
, you'd put the first question returned from the database into the 5th row, the second question into the 18th row, the third question into the 11th row, and so forth. The fifteen 10-point questions would always use the first fifteen numbers in your 1-through-20 permutation, and the five 20-point questions would always use the last five numbers, but that doesn't matter, since the permutation is random.
Upvotes: 1