There are several ways to do this. The first thing that comes to mind: 1. <cfset randomstring=dateformat(now(),"mmddyyyy") & timeformat(now(),"hhmmsstt") & randrange(1,1000)>
It will never be this day, year, month, hour, minute and second again - you're safe here. The possibility of your server generating more than 1000 at the exact same split second is very unlikely. OR Using the database would be ideal, but if you truly need to generate the random number without aid of db just use a few different random strings of random length and randomize their order - just to make sure it's next to impossible to have a match. Something like this: 1. Generate a random number (3-6) of random strings each a random length (8-15 chars) using my RandString() function at CFLib.org 2. Use my ListJuxt() function at CFLib.org to "shuffle" the 3 strings together. 3. "Cut the deck" using left and right string functions with random lengths and then reassemble before inserting the string. 4. If you want the string to always be the same length then use MID() with a random start point that gives the correct length. Alternatively you could first grab the recordcount of your table, generate a random string and then append the recordcount+1 to the end of the string. Yes, that's convoluted, but it would put probability on your side. Best bet is to let your database handle it though. Joshua Miller Head Programmer / IT Manager Garrison Enterprises Inc. www.garrisonenterprises.net [EMAIL PROTECTED] (704) 569-9044 ext. 254 ************************************************************************ ************* Any views expressed in this message are those of the individual sender, except where the sender states them to be the views of Garrison Enterprises Inc. This e-mail is intended only for the individual or entity to which it is addressed and contains information that is private and confidential. If you are not the intended recipient you are hereby notified that any dissemination, distribution or copying is strictly prohibited. If you have received this e-mail in error please delete it immediately and advise us by return e-mail to [EMAIL PROTECTED] ************************************************************************ ************* -----Original Message----- From: Bryan Stevenson [mailto:[EMAIL PROTECTED] Sent: Thursday, March 20, 2003 1:30 PM To: CF-Talk Subject: Re: Unique random number? Nobody said the initial logic was good ;-) That's a good point. Why does the unique ID have to be random? Why isn't your DB automatically generating a new unique ID (sequence, identity, or auto numbering depending on DB)? Bryan Stevenson B.Comm. VP & Director of E-Commerce Development Electric Edge Systems Group Inc. t. 250.920.8830 e. [EMAIL PROTECTED] --------------------------------------------------------- Macromedia Associate Partner www.macromedia.com --------------------------------------------------------- Vancouver Island ColdFusion Users Group Founder & Director www.cfug-vancouverisland.com ----- Original Message ----- From: "Ezine" <[EMAIL PROTECTED]> To: "CF-Talk" <[EMAIL PROTECTED]> Sent: Thursday, March 20, 2003 10:18 AM Subject: RE: Unique random number? > Eventually though.. this will cause a page to loop many times.. and run > the Query many times increasing load time and processor time. > > -Zine > -----Original Message----- > From: Bryan Stevenson [mailto:[EMAIL PROTECTED] > Sent: Thursday, March 20, 2003 1:14 PM > To: CF-Talk > Subject: Re: Unique random number? > > > I'm having trouble seeing why you need a loop for this?? Can you > explain? > > From your description I would think you'd follow these steps > (conditional statements and not loops): > > 1) set your random number (as you have) > 2) run a query to see if that number is used in either table (simple SELECT > statement) > 3) IF there is a recordcount to the above query then it's in use - do > whatever you need to do to handle this. IF NO recordcount then use it > as your new job number to create the record(s) you need. > > HTH > > Cheers > > Bryan Stevenson B.Comm. > VP & Director of E-Commerce Development > Electric Edge Systems Group Inc. > t. 250.920.8830 > e. [EMAIL PROTECTED] > > --------------------------------------------------------- > Macromedia Associate Partner > www.macromedia.com > --------------------------------------------------------- > Vancouver Island ColdFusion Users Group > Founder & Director > www.cfug-vancouverisland.com > ----- Original Message ----- > From: <[EMAIL PROTECTED]> > To: "CF-Talk" <[EMAIL PROTECTED]> > Sent: Thursday, March 20, 2003 9:55 AM > Subject: Unique random number? > > > > Need to set a job number to a unique number, based of creating a > > number using the RandRange function, but then I need to take that > > number and bounce it off two access tables (called openjobs & > > closejobs) to see if that random number is already used, then loop > > back if it is to start over, > > but if it isn't, then use it to create a new record in openjobs > > table. > > > > What I don't know how to do is the loop structure if the number > > already exists on one of the two tables. > > > > Could someone direct me in how the code would look starting with > > this > CFSET > > statement? > > > > <CFSET job_number = RandRange(1, 100000)> > > > > ...and as always, thanks in advance! > > > > > > > > ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~| Archives: http://www.houseoffusion.com/cf_lists/index.cfm?forumid=4 Subscription: http://www.houseoffusion.com/cf_lists/index.cfm?method=subscribe&forumid=4 FAQ: http://www.thenetprofits.co.uk/coldfusion/faq Get the mailserver that powers this list at http://www.coolfusion.com Unsubscribe: http://www.houseoffusion.com/cf_lists/unsubscribe.cfm?user=89.70.4

