How about this:

run the query,
use randrange(1, query.recordcount)
then you can do just about anything to get that record number - no matter
what the unique ID is...

ie:
<cfset RN = randrange(1, query.recordcount)>
#query.firstname[RN]#

That would display the first name of the randomly selected record...

Jason Egan
Konnections, INC.

-----Original Message-----
From: Gary McNeel, Jr. [mailto:[EMAIL PROTECTED]]
Sent: Monday, June 12, 2000 12:20 PM
To: Cf-Talk
Subject: Easy way to get a random record

Is there an easy way to pick a random record from a database. I have a
unique ID for each record, but it is not strictly sequential because of
record deletions.

Here is my thought:

1. Run a query and get the MAX and MIN IDs.
2. Do a loop using them as the beginning and ending range and generate a
random
number between them.
3. See if that number exists in the ID field of the db.
4. If yes, get the record, if not, try again.
5. Do this until you get a hit.
6. Alternatively I guess you could gen a random number and take the next
higher
number unless it is the highest number.

That seems way to intensive and I know there must be a cleaner way. The
programmers must know a good way to do this.

Any thoughts or SQL code would be greatly appreciated.

Thanks.

Gary McNeel, Jr.
Project Manager - DAC-Net, Research & Graduate Studies
Rice University - Houston
[W] 713-348-5184
[M] 713-962-0885
[H] 713-723-9240

"One good thing about apathy is you don't have to exert yourself to show
you're sincere about it."

----------------------------------------------------------------------------
--
Archives: http://www.eGroups.com/list/cf-talk
To Unsubscribe visit
http://www.houseoffusion.com/index.cfm?sidebar=lists&body=lists/cf_talk or
send a message to [EMAIL PROTECTED] with 'unsubscribe' in
the body.

------------------------------------------------------------------------------
Archives: http://www.eGroups.com/list/cf-talk
To Unsubscribe visit 
http://www.houseoffusion.com/index.cfm?sidebar=lists&body=lists/cf_talk or send a 
message to [EMAIL PROTECTED] with 'unsubscribe' in the body.

Reply via email to