Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

random question

Status
Not open for further replies.

gheidi

Technical User
Feb 1, 2004
2
0
0
NO
Hi! Using access and asp I would like to make a site where you get one question at a time, 20 questions before you get your result. In my database there are 40 questions. So I need to work out how I can make my site choose a random question each time. I've managed to make it choose one, but the answers doesn't match the question.
Can anyone help?
Thank you.
- Heidi
 
Hi Heidi, this should get you going...

STEP 1.
Fetch all the questions and answers and store them in Application state in arrays. Something like this:

Application("QUESTIONS") = Array("q1", "q2", "q3", ...)
Application("ANSWERS") = Array("a1", "a2", "a3", ...)

Do this in Application_OnStart() in global.asa

STEP 2.
In Session_OnStart() in global.asa, create a list of 40 sequential numbers and randomize them. Store them in Session state in an array. For example:

Sub Session_OnStart()
Session("QUESTION_ORDER") = RandomizeSequentialNumbers(0, 39)
End Sub

The functions for randomizing are below.

STEP 3.
In the ASP page, to get the 20 random questions, do something like:

For i = 0 to 19
Response.Write Application(&quot;QUESTIONS&quot;)(Session(&quot;QUESTION_ORDER&quot;)(i)) & &quot;<br/>&quot;
Next




Function RandomizeSequentialNumbers(LowerBound, UpperBound)
Dim dictList, varOrderedRandomList, i, j

Set dictList = Server.CreateObject(&quot;Scripting.Dictionary&quot;)

Randomize Timer
For i = LowerBound To UpperBound
dictList.Add Rnd(), i
Next

varOrderedRandomList = BubbleSortNumbers(dictList.Keys)

For j = 0 To UBound(varOrderedRandomList)
varOrderedRandomList(j) = dictList(varOrderedRandomList(j))
Next

Set dictList = Nothing

RandomizeSequentialNumbers = varOrderedRandomList
End Function


Function BubbleSortNumbers(List)
Dim i, j, varTemp, boolSwapped

i = UBound(List)

Do While i >= 0
boolSwapped = False
For j = 0 To i - 1
If List(j) > List(j + 1) Then
varTemp = List(j)
List(j) = List(j + 1)
List(j + 1) = varTemp
boolSwapped = True
End If
Next

If Not boolSwapped Then
Exit Do
End If
Loop

BubbleSortNumbers = List
End Function
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top