Click to See Complete Forum and Search --> : Multi-dimensional Arrays


Nicodemas
03-27-2003, 05:48 AM
Here's the situation:
I have a database that has 45 questions, and four Answer fields (Answer1, Answer2, Answer3, Answer4).
Another field is called CorrectAnswer (which is always set to one because the correct answer is always Answer1's value.

Thing is, I want to display these questions in random order.
Well, I've got that part down with the code below, but sometimes it repeats a question.
This is what I am trying NOT to do.

I thought about sticking the random number in an array and test for duplicates, but I can't seem to do it, and that seems a bit overly complicated.

Think you can get it workin'? My brain is toast...

<%
Randomize Timer ' SET Rnd SEED = TIMER
intTotalQ = RS.RecordCount 'RETURNS THE TOTAL NUMBER OF RECORDS IN THE RECORDSET

For x = 1 To intTotalQ
intRnd = (x * rnd)+rnd ' SET intRnd = A RANDOM NUMBER BETWEEN 1 AND 45
RS.Move intRnd 'MOVE TO THAT RANDOM NUMBER

Response.Write("<TR>") ' WRITE OUT A TABLE ROW
Response.Write("<TD ID='Ques'>") 'GIVE IT THE GREEN QUESTION COLOR
Response.Write(x & ". " & RS("Question")) 'WRITE THE QUESTION
Response.Write("</TD>") 'CLOSE THE CELL UP
Response.Write("</TR><TR>") 'START NEW ROWS
Response.Write("<TD ID='Ans'>") ' FORMAT IT WITH THE ANSWER STYLE (THE OLIVE COLOR)
For i = 1 To 4 'FOR EACH FIELD NAMED "ANSWER#"
If Not RS("Answer"&i) = "" Then 'CHECK IF THE FIELD IS -NULL-. IF IT IS NOT -NULL-
Response.Write("<INPUT TYPE='radio' NAME='strAns" & RS("QuestionID") & "' VALUE='" &_
& RS("Answer"&i) & "'>" & RS("Answer"&i) & "<BR>") 'WRITE A RADIO W/ ANSWER[i]
End If
Next
Response.Write("</TD></TR>")
If RS.EOF="True" Then 'IF THE RANDOM NUMBERS RUN PAST THE intTotalQ
x= x-1 'SUBTRACT ONE FROM THE COUNT
RS.MoveFirst 'MOVE TO THE FIRST RECORD IN THE SET
End If
Next
%>

Nicodemas
03-28-2003, 05:34 AM
bump?

DaiWelsh
03-28-2003, 06:17 AM
Assuming that your database does not support random ordering itself (have you checked? It would be much easier for you if it does). I would suggest that you load the records into an array then either randomise the sequence of the array and read straight through it or iteratively pick an item from the array at random then delete it from the array. The last part is the key bit to avoid duplicates and is where your original code fails.

You might be able to achieve the same thing by deleting the record from your rcordset after itis selected, but I am not in front of an ADO reference just now so not sure how straightforward that is.

Nicodemas
03-31-2003, 08:04 AM
Thank you for responding.

I tried throwing in an "RS.Delete adAffectCurrent" at the end of the parent For... Loop statement, but it doesn't seem to work. I still get duplicates. Suggestions?

Ribeyed
03-31-2003, 05:08 PM
hi,
why don't you remove the duplicates during your sql statement?

Nicodemas
04-01-2003, 05:06 AM
There are no duplicates in the database, only when the loop finishes, if it reaches EOF it starts over, and then there is the chance of getting a duplicate.

At the moment, I'm experimenting with loading them into an array, so bear with me as I test new stuff. :)

Thanks all.

Nicodemas
04-01-2003, 06:06 AM
-edit-