Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Random 6 digit code

Posted on 2006-07-20
5
Medium Priority
?
746 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

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

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…
When it comes to showing a 404 error page to your visitors, you do not want that generic page to show, and you especially do not want your hosting provider’s ad error page to show either. In this article, I will show you how to enable the custom 40…
In this video you will find out how to export Office 365 mailboxes using the built in eDiscovery tool. Bear in mind that although this method might be useful in some cases, using PST files as Office 365 backup is troublesome in a long run (more on t…
In this video, Percona Solution Engineer Dimitri Vanoverbeke discusses why you want to use at least three nodes in a database cluster. To discuss how Percona Consulting can help with your design and architecture needs for your database and infras…

610 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