> and yeah bobby I USE IT ALL THE DAMN TIME
to generate random numbers? Wow, what a waste of processing... RandRange()
works just fine for numbers... as for uniqueness, you can only make sure the
number isn't already in use by checking your database. If it is in use,
generate another and check it. Wash, rinse, repeat until the number isn't
being used.
<!--- generate a random number --->
<cfset thisRandNum = Randrange(0, 999999) />
<!--- check to see if the number is in the database --->
<cfquery name="numCheck" datasource="myDSN">
Select recordID from table where randomnumberfield = #val(thisRandnum)#
</cfquery>
<!--- if the number is in the database, repeat all of the above until it is
NOT used in the db --->
<cfloop condition="numCheck.recordcount GT 0">
<cfset thisRandNum = Randrange(0, 999999) />
<cfquery name="numCheck" datasource="myDSN">
Select recordID from table where randomnumberfield =
#val(thisRandnum)#
</cfquery>
</cfloop>
#thisRandNum#
At best, this is just taking a set amount of numbers and scrambling them...
it will become less and less efficient (if you can call it efficient) with
each new record added to your database. If you explain a little more about
what it is you are doing and why, I'm sure someone can offer a better
solution.
..:.:.:.:.:.:.:.:.:.:.:.
Bobby Hartsfield
http://acoderslife.com
..:.:.:.:.:.:.:.:.:.:.:.
Bobby Hartsfield
http://acoderslife.com
-----Original Message-----
From: Dave l [mailto:[EMAIL PROTECTED]
Sent: Monday, February 11, 2008 5:48 PM
To: CF-Talk
Subject: Re: Generating unique random numbers
Bobby has a nice lil udf for that and yeah bobby I USE IT ALL THE DAMN
TIME!! haha
>Is there a slick way to do this in CF? Let's say I have a set of
>records - one field of which is for a unique and random 6 digit
>number. When adding a new record, what the best way to assign its
>random number that's not already assigned to another record?
>
>Thanks in advance,
>Les
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~|
Adobe® ColdFusion® 8 software 8 is the most important and dramatic release to
date
Get the Free Trial
http://ad.doubleclick.net/clk;160198600;22374440;w
Archive:
http://www.houseoffusion.com/groups/CF-Talk/message.cfm/messageid:298785
Subscription: http://www.houseoffusion.com/groups/CF-Talk/subscribe.cfm
Unsubscribe:
http://www.houseoffusion.com/cf_lists/unsubscribe.cfm?user=11502.10531.4