Yeah that’s it. Thanks.
 
..:.:.:.:.:.:.:.:.:.:.:.:.:.:.:.:.:.:.:.
Bobby Hartsfield
http://acoderslife.com
 
 

-----Original Message-----
From: Andy Matthews [mailto:[EMAIL PROTECTED] 
Sent: Monday, December 19, 2005 12:50 PM
To: CF-Talk
Subject: RE: Random Numbers

mySQL is

SELECT some_column
FROM some_table
LIMIT 3
ORDER BY RAND()

<!----------------//------
andy matthews
web developer
ICGLink, Inc.
[EMAIL PROTECTED]
615.370.1530 x737
--------------//--------->

-----Original Message-----
From: Bobby Hartsfield [mailto:[EMAIL PROTECTED]
Sent: Monday, December 19, 2005 11:33 AM
To: CF-Talk
Subject: RE: Random Numbers


What database are you using?

In SQL server you can select the top 3 and order by newid()

I'm pretty sure you can do that in MySQL too... I'm just not sure if it's
actually 'newid()' in MySQL

-----

Else, I can think of a couple different ways to do it....

The simplest way I can think of...

Get a list of all the IDS from the table

So you'd have a list of IDS like  "1,2,3,4,5,6,7,8,9"

Then loop from 1 to however many records you want while grabbing a random ID
from the list each time and removing it from the list to ensure it doesn’t
get selected again...

<cfset randomids = "">
<cfset numberofrandomrecords = 3>

<cfloop from=-"1" to=numberofrandomrecords index="i">

<!--- get a random number in the range of list items --->
<cfset thisposition = randrange(1, listlen(listofids))>

<!--- get the id in that position of the list --->
<cfset randomids = listappend(randomids, listgetat(listofids,
thisposition))>

<!--- delete that list item so its not selected again --->
<cfset listdeleteat(listofids, thisposition)>

</cfloop>

When its done, #randomids# will hold 3 random ids then you just
select * from table where id in (listofids)

That's all untested of course and knowing me... there's a typo somwhere...


If there are a ton of records and you don’t want to grab all the ids
everytime... just grab half or something and randomly order them by
different columns and/or asc/desc

Eg...

<cfset columnlist = "ID,fname,lname,bday">
<cfset directionlist = "asc,desc">
<cfset orderby = listgetat(columnlist, randrange(1, listlen(columnlist)))>
<cfset orderdirection = listgetat(directionlist, randrange(1, 2)))>

<cfquery name="qry" datasource="dsn" maxrows="20">
Select ID from table
Orderby #orderby# #orderdirection#
</cfquery>

....:.:.:.:.:.:.:.:.:.:.:.:.:.:.:.:.:.:.:.
Bobby Hartsfield
http://acoderslife.com



-----Original Message-----
From: Stuart Kidd [mailto:[EMAIL PROTECTED]
Sent: Monday, December 19, 2005 11:32 AM
To: CF-Talk
Subject: Random Numbers

Hi guys,

I'm trying to write an app which will choose three random numbers for a
competition based on how many etnries there are.  I'm having problems trying
to work out a way not to choose the same number twice, is there a way to do
that which will fit into my CFLOOP structure?  Or is there a better way to
do it?








~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~|
Logware (www.logware.us): a new and convenient web-based time tracking 
application. Start tracking and documenting hours spent on a project or with a 
client with Logware today. Try it for free with a 15 day trial account.
http://www.houseoffusion.com/banners/view.cfm?bannerid=67

Message: http://www.houseoffusion.com/lists.cfm/link=i:4:227284
Archives: http://www.houseoffusion.com/cf_lists/threads.cfm/4
Subscription: http://www.houseoffusion.com/lists.cfm/link=s:4
Unsubscribe: 
http://www.houseoffusion.com/cf_lists/unsubscribe.cfm?user=11502.10531.4
Donations & Support: http://www.houseoffusion.com/tiny.cfm/54

Reply via email to