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 derfloh on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Randomly pick a record

Status
Not open for further replies.

Andel

Programmer
Feb 15, 2001
366
US
This is my first asp project.

TblRiddles
RiddleId TheRiddle
-------- --------------------------
1 My Riddle Number1
2 My Riddle Number2
3 My Riddle Number3


Tbl_Answers
AnswerID RiddleId TheAnswer
-------- -------- ----------------------
1 1 Answer to Riddle#1
2 1 Also an Answer to riddle #1
3 2 Answer to Riddle#2
4 3 Answer to #3


What I want to do is to display 1 riddle at a time and give visitors a text box to place their answers. Then When their answer is correct, I want to display the next riddle. I'm thingking of making my ASP randomly pick a riddle.

Any suggestion?

Well, I know how to display the first riddle and the text box to place answers, but I don't know how to go to the next or randomly picked riddle.

Thanks in advance.

Andel
andel@barroga.net
 
Hey Andel!

The following code will get you a random positive number between 0 and 10:

=====================================================
Dim Tools
Set Tools = Server.CreateObject("MSWC.Tools")

Response.Write (Abs(Tools.Random)) Mod 10
=====================================================

The hard part is keeping this project efficient. You could do a query on the TblRiddles to find out how many records there are, then get a random number from 0 to RiddleCount. This random number could be the amount of times you step through the recordset before stopping and displaying the riddle. Probably not the most efficient solution, but as long as you don't end up with thousands of riddles it would work ok.

It would be better though, to just move straight to the position in the recordset that you want to display, with the RS.Move() method... ie.

sql = "select Count(*) As NumRecs from tblriddles;"
Set RS = DataConn.Execute(sql)


If not RS.EOF then
Dim NumRiddles, RandNum
NumRiddles = RS("NumRecs")

'Get random number
RandNum = (Abs(Tools.Random)) Mod NumRiddles

'Move to position in recordset
RS.Move RandNum

Response.write "Random Riddle: " & RS("RiddleFields")

End if


Hope this helps!
Brett Birkett B.Comp
Systems Analyst
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top