Solved

Random 6 digit code

Posted on 2006-07-20
5
646 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
Comment Utility
Isn't there anyone here who can help?

Thanks

Chris
0
 
LVL 7

Accepted Solution

by:
aseusainc earned 500 total points
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

One of the typical problems I have experienced is when you have to move a web server from one hosting site to another. You normally prepare all on the new host, transfer the site, change DNS and cross your fingers hoping all will be ok on new server…
If you don't have the right permissions set for your WordPress location in IIS, you won't be able to perform automatic updates. Here's how to fix the problem.
Internet Business Fax to Email Made Easy - With eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, fr…
This video explains how to create simple products associated to Magento configurable product and offers fast way of their generation with Store Manager for Magento tool.

771 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

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now