I would first run the query and get the total record count.
<cfquery name="myQuery" datasource="myData">
SELECT ID
FROM MyTable
</cfquery>
I would then do a
<cfset myVar = RandRang(1, myQuery.recordcount)>
Then I would run another query this time I would use my select as with the
myVar from the above step. You could scope the first query so your not
running it each time and that way you just create a random number each time
and then that user would get a random record. Of course the more records,
the more random.
<cfquery name="myRandQuery" datasource="myData">
SELECT ID
FROM MyTable
WHERE ID = myVar
</cfquery>
Hope that helps
Bill
CF_Warrior
PS. I don't know if you already got a response on this as the list has been
wacky lately, but just in case.
> -----Original Message-----
> From: Gary P. McNeel, Jr. [mailto:[EMAIL PROTECTED]]
> Sent: Thursday, June 08, 2000 9:41 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:
>
> Run a query and get the MAX and MIN IDs.
> Do a loop using them as the beginning and ending range and
> generate a random
> number between them.
> See if that number exists in the ID field of the db.
> If yes, get the record, if not, try again.
> Do this until you get a hit.
> 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.