Solved

Random 6 digit code

Posted on 2006-07-20
5
661 Views
Last Modified: 2013-12-24
Hello,

I need to generate 250,000 6 digit unique random codes.

The codes must contain any alphanumeric characters but it must contain at least one of each.

These need to be placed into an access db to send to a print company.

I know coldfusion has several random functions but how to put them into practice is another thing entirely plus, i'm not sure how to stop duplicates and make sure every code has at least 1 number and 1 letter.

Can someone either show me an example, or point me to a place?

Thanks in advance

Chris
0
Comment
Question by:chrissp26
  • 3
  • 2
5 Comments
 
LVL 2

Author Comment

by:chrissp26
ID: 17145222
Isn't there anyone here who can help?

Thanks

Chris
0
 
LVL 7

Accepted Solution

by:
aseusainc earned 500 total points
ID: 17145311
I'll give it a go.  I may or may not have overengineered this, but it was fun to code regardless.  Loop this 250000 times hehehe:

<cfset numbers="0,1,2,3,4,5,6,7,8,9">
<cfset letters="A,B,C,D,E,F,G,H,I,J,K,L,M,N,O,P,Q,R,S,T,U,V,W,X,Y,Z">

<!--  MUST contain a number  -->
<cfset digit1 = listgetat(numbers,randrange(1,10))>
<cfset thecode="#digit1#">

<!--  MUST contain a letter  -->
<cfset digit2 = listgetat(letters,randrange(1,26))>

<!--  Add a little more randomness by not proceeding sequentially,
rather giving each digit a chance to be in front of or behind the
previous character  -->
<cfset pickmode=randrange(1,2)>
<cfif pickmode EQ 1>
  <cfset thecode = listprepend(thecode,digit2)>
<cfelse>
  <cfset thecode = listappend(thecode,digit2)>
</cfif>

<!--  Now that we have our mandatory 1 letter and 1 number,
we can be even more random about which list we pick from  -->
<cfset picklist=randrange(1,2)>
<cfif picklist EQ 1>
  <cfset digit3 = listgetat(numbers,randrange(1,10))>
<cfelse>
  <cfset digit3 = listgetat(letters,randrange(1,26))>
</cfif>
<cfset pickmode=randrange(1,2)>
<!--  And again, lets randomize where we stuff this into the sequence  -->
<cfif pickmode EQ 1>
  <cfset thecode = listprepend(thecode,digit3)>
<cfelse>
  <cfset thecode = listappend(thecode,digit3)>
</cfif>

<!--  4th digit  -->
<cfset picklist=randrange(1,2)>
<cfif picklist EQ 1>
  <cfset digit4 = listgetat(numbers,randrange(1,10))>
<cfelse>
  <cfset digit4 = listgetat(letters,randrange(1,26))>
</cfif>
<cfset pickmode=randrange(1,2)>
<cfif pickmode EQ 1>
  <cfset thecode = listprepend(thecode,digit4)>
<cfelse>
  <cfset thecode = listappend(thecode,digit4)>
</cfif>

<!--  5th digit  -->
<cfset picklist=randrange(1,2)>
<cfif picklist EQ 1>
  <cfset digit5 = listgetat(numbers,randrange(1,10))>
<cfelse>
  <cfset digit5 = listgetat(letters,randrange(1,26))>
</cfif>
<cfset pickmode=randrange(1,2)>
<cfif pickmode EQ 1>
  <cfset thecode = listprepend(thecode,digit5)>
<cfelse>
  <cfset thecode = listappend(thecode,digit5)>
</cfif>

<!--  6th digit  -->
<cfset picklist=randrange(1,2)>
<cfif picklist EQ 1>
  <cfset digit6 = listgetat(numbers,randrange(1,10))>
<cfelse>
  <cfset digit6 = listgetat(letters,randrange(1,26))>
</cfif>
<cfset pickmode=randrange(1,2)>
<cfif pickmode EQ 1>
  <cfset thecode = listprepend(thecode,digit6)>
<cfelse>
  <cfset thecode = listappend(thecode,digit6)>
</cfif>

<cfoutput>#replacenocase(thecode,',','','ALL')#</cfoutput>
0
 
LVL 2

Author Comment

by:chrissp26
ID: 17145360
Pure Genius!!!

Thanks ever so much for your help. I'm going to sit down for the next four years and study what you have done :)

Thanks for your help.

Chris
0
 
LVL 7

Expert Comment

by:aseusainc
ID: 17145424
Pretty simple stuff really.

1st, we know that we MUST have 1 number and 1 letter.  So digit1 we pick from a list of numbers and start a list with the value, digit2 we pick from a list of letters then randomly insert it at either the front or the back of the list.  Then we just proceed through 3-6.  We randomly pick which list to choose from (letters or numbers), then randomly pick a value, then randomly put it in front of or behind the code list.  At the end, I'm just removing the commas from the list to make a string.

Theres no checking for dupes, but it would be a pretty slim chance of getting a dupe.  You can either code something in, which is going to add a TON of processing overhead, as you'll need to check each value against the database.  If you want to do this, let us know.

otherwise, just do a

SELECT DISTINCT thecode
FROM   tblCodes

and make sure the rowcount is 250000.  If you come up with less than 250000, you got a dupe.

Thanks for the points.  Good luck w/ your project!
0
 
LVL 2

Author Comment

by:chrissp26
ID: 17146978
Thanks for that, I used a little SQL to check for Dupes and double checked it with the DISTINCT query as well to make sure the number I got from the first added to the number I got from the second equalled the total ammount.

So it balanced.

Thanks again for your help I am extremly grateful

Chris
0

Featured Post

Complete VMware vSphere® ESX(i) & Hyper-V Backup

Capture your entire system, including the host, with patented disk imaging integrated with VMware VADP / Microsoft VSS and RCT. RTOs is as low as 15 seconds with Acronis Active Restore™. You can enjoy unlimited P2V/V2V migrations from any source (even from a different hypervisor)

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Have you ever sent email via ColdFusion and thought of tracking this mail to capture the exact date and time when the message was opened ?  If yes, then this article is for you ! First we need a table user_email with columns user_id , email , sub…
Article by: kevp75
Hey folks, 'bout time for me to come around with a little tip. Thanks to IIS 7.5 Extensions and Microsoft (well... really Windows 8, and IIS 8 I guess...), we can now prime our Application Pools, when IIS starts. Now, though it would be nice t…
Established in 1997, Technology Architects has become one of the most reputable technology solutions companies in the country. TA have been providing businesses with cost effective state-of-the-art solutions and unparalleled service that is designed…
A short tutorial showing how to set up an email signature in Outlook on the Web (previously known as OWA). For free email signatures designs, visit https://www.mail-signatures.com/articles/signature-templates/?sts=6651 If you want to manage em…

785 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question