Random | Statement building randomly generated HTML code is a fairly easy to implement ASP feature. You may have created a "daily post", a scrolling ad, and so on, just a little embellishment will make your site long and new.
For data stored in the database, the random number feature gives the effect, but they may be too slow. You can't ask an ASP to "find a random number" and print it out. In fact, the common solution is to create a loop that looks like this:
Randomize
Rnumber = Int (rnd*499) +1
While not objrec.eof
If Objrec ("ID") = Rnumber THEN
... Here is the execution script ...
End If
Objrec.movenext
Wend
It's easy to understand. First, you take out a random number from 1 to 500 (assuming 500 is the total number of records in the database). Then, you traverse each record to test the ID's value and check to see if it matches the rnumber. The code that starts with the then keyword is executed if the condition is met. If your rnumber equals 495, then it takes a long time to cycle through the database. Although the 500 figure looks bigger, it's a small database compared to a more robust enterprise solution, which typically contains tens of thousands of records in a single database. Is this the right time to die?
With SQL, you can quickly find the exact record and open a recordset that contains only the record, as follows:
Randomize
Rnumber = Int (rnd*499) + 1
SQL = "SELECT * from Customers WHERE ID =" & Rnumber
Set Objrec = objConn.Execute (SQL)
Response.writernumber & "=" & Objrec ("ID") & "& Objrec (" C_email ")
You don't have to write Rnumber and IDs, you just need to check the match. As long as you are satisfied with the above code work, you can operate "random" records on demand. The recordset does not contain other content, so you will soon be able to find the records you need so that the processing time is greatly reduced.
Talk about random number again
Now that you are determined to squeeze the last drop of the random function, you may be able to take out more than one random record at a time or want to use a certain random range of records. By extending the standard random example above, you can use SQL to deal with both of these situations.
To take a few randomly selected records and store them in the same recordset, you can store three random numbers and then query the database for records that match these numbers:
SQL = "SELECT * from Customers WHERE id =" & rnumber & "or id =" & RNumber2 & "or id =" & rnumber 3
If you want to select 10 records (perhaps a list of 10 links each time the page is loaded), you can use between or mathematical equations to select the first record and the appropriate number of incremental records. This can be done in several ways, but the SELECT statement only shows one possibility (where the ID is an automatically generated number):
SQL = "SELECT * from Customers WHERE ID BETWEEN" & Rnumber & "and" & Rnumber & "+ 9"
Note: The above code is not intended to check whether there are 9 concurrent records in the database. If you need to ensure that 10 records are selected each time, you must further design the query.