?
Solved

Random 6 digit code

Posted on 2006-07-20
5
Medium Priority
?
724 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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 2000 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

Get MongoDB database support online, now!

At Percona’s web store you can order your MongoDB database support needs in minutes. No hassles, no fuss, just pick and click. Pay online with a credit card. Handle your MongoDB database support now!

Question has a verified solution.

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

Periodically we have to update or add SSL certificates for customers. Depending upon your hosting plan you may be responsible for the installation and/or key generation. In the wake of Heartbleed many sites were forced to re-key. We will concen…
Meet the world's only “Transparent Cloud™” from Superb Internet Corporation. Now, you can experience firsthand a cloud platform that consistently outperforms Amazon Web Services (AWS), IBM’s Softlayer, and Microsoft’s Azure when it comes to CPU and …
This is my first video review of Microsoft Bookings, I will be doing a part two with a bit more information, but wanted to get this out to you folks.
Do you want to know how to make a graph with Microsoft Access? First, create a query with the data for the chart. Then make a blank form and add a chart control. This video also shows how to change what data is displayed on the graph as well as form…

764 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