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

Random function that uses only existing posts in database

Status
Not open for further replies.

Jahappz

Technical User
Jun 3, 2002
133
SE
Hello!

I have a asp page with a .mdb database file
that has a table with the following structure:

People
-ID
-Name

i want to create a random function that returns a different number (within existing posts) each time the page is visited.

The function below random returns a number from 1-6 but i want it only to return an existing id from the database and also set the id range from "first id in database to last id in database"

Any one knows how to do this?

<%
Function RandomNumber(intHighestNumber)
Randomize
RandomNumber = Int(Rnd * intHighestNumber) + 1
End Function

Response.Write(RandomNumber(6))
%>

/Andreas
 
I used this to display a random tip of the day on an app of mine. Basically you count the number of records then loop through to a random number that is equal to or less than the number of records then display that record.

<%
TBL.Open "SELECT count(*) AS Total FROM Tips", DB
Total=TBL("Total")
TBL.Close

Randomize
RandomTipNo=int(rnd*Total)

TBL.Open "SELECT * FROM Tips", DB

For i=1 to RandomTipNo
TBL.MoveNext
Next

Response.Write(TBL("Tip"))

TBL.Close
Set TBL=Nothing
Set DB=Nothing
%>
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top