Solved

Random 6 digit code

Posted on 2006-07-20
5
670 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

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Problem to Eclipse 16 132
Problem to get function 52 113
Websocket through haproxy reused connection causes "Not a valid frame" error. 4 108
Link failure 16 37
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…
Introduction This article explores the design of a cache system that can improve the performance of a web site or web application.  The assumption is that the web site has many more “read” operations than “write” operations (this is commonly the ca…
The Email Laundry PDF encryption service allows companies to send confidential encrypted  emails to anybody. The PDF document can also contain attachments that are embedded in the encrypted PDF. The password is randomly generated by The Email Laundr…

821 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