Generate Unique serial numbers

Posted on 2004-11-08
Last Modified: 2013-12-24
I'm looking for a way to generate unique serial numbers like you would find on a software product..

i have a few requirements.

1) the number needs to be generated on the fly
2) it must be unique on the host machine, if it can be globally unique even better
3) it should contain only the charachters "123456789ABCDEFGHJKLMNPQRSTUWXYZ", so as to avoid mis-typing them
4) the key should be no more than 20 characters long, and always the same length
5) serial numbers will be validated against the DB, so it does not require a checksum

So far i've experimented with hashing uuid's and trying to convert them to different base systems, but i'm not sure whether the results i'm creating are still unique.

i'm happy to create the keys using cfscript or in a java object that i can instantiate in my CF code

any ideas..

Question by:monkeychump
    LVL 35

    Accepted Solution

    Have you thought about using CreateUUID

    It has only hex digits ( 0 throgh 9 and A through F)

    But it is 35 characters long

    Otherwise it meets all of your requirements.  

    Maybe you could use 35 characters or somehow use only a portion of the generated value (although I don't know what unique constraints you would hit that way.

    Maybe you could use the 1st 20 chars and do a test to see if it is already in the DB - if so then generate a new one.  Chances are that you will not incurr this penalty too often...

    LVL 1

    Author Comment

    i don't want to use a plain uuid, because there values look vaguely sequential, and it is essential that this key cannot just be guessed.

    I have MD5 hashed a uuid which gives a 32 character hex string, which is more "random" in its appearance, but what i was hoping to do was shorten that to 20 characters (with a larger caracter set), but retain its uniqueness..

    LVL 6

    Assisted Solution

    Does the validating DB contain all generated serial numbers? If so, you could do something like this (in pseudo code):

    generated_code = generate 20 char code using "0-9A-Z"
    while (select codecolumn from table where codecolumn = generated_code)

    LVL 7

    Assisted Solution

    The following code will generate 20-char ID with very high probability of uniqueness:

    <cfset a=Replace(CreateUUID(), "-", "", "ALL")>
    <cfset set="123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ">
    <cfset str="">
    <cfloop index="i" from="1" to="4">
       <cfset b=InputBaseN(Left(a, 8), 16)>
       <cfif b LT 0>
          <cfset b=-b>
       <cfset a=RemoveChars(a, 1, 8)>
       <cfloop index="j" from="1" to="5">
          <cfset c=b MOD 35>
          <cfset b=b/35>
          <cfset str=str & Mid(set, c+1, 1)>
       <cfset str=str & "-">
    <cfset str=Left(str, 23)>

    LVL 8

    Assisted Solution

    Or try this.  I am doing something similar but for a much shorter string for generating security passwords.

    function getRandString(stringLength,spacerlength) {
          var tempAlphaList = "a,b,c,d,e,g,h,i,k,l,m,n,o,p,q,r,s,t,u,v,w,x,y,z";
          var tempNumList = "1,2,3,4,5,6,7,8,9,0";
          var tempCompositeList = tempAlphaList&""&tempNumList;
          var tempCharsInList = listLen(tempCompositeList,",");
          var tempCounter = 1;
          var tempWorkingString = "";
          //loop from 1 to stringLength to generate string
          while (tempCounter LTE stringLength+spacerlength) {
                if ((tempCounter mod spacerlength is 0) and (tempCounter LT stringLength+spacerlength)){
                      tempWorkingString = tempWorkingString&"-";}
                else if (tempCounter LT stringLength+spacerlength) {
                      tempWorkingString = tempWorkingString&listGetAt(tempCompositeList,randRange(1,tempCharsInList),",");
                tempCounter = tempCounter + 1;
          return tempWorkingString;

    cfset licensenum = ucase(getRandString(20,5))#

    Query your table for  licensenum, if it exists then loop til recordcount is 0.

    Also just changed the number values to change the length of the string and the position of the -

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    Highfive Gives IT Their Time Back

    Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

    A web service ( is a software related technology that facilitates machine-to-machine interaction over a network. This article helps beginners in creating and consuming a web service using the ColdFusion Ma…
    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…
    Hi everyone! This is Experts Exchange customer support.  This quick video will show you how to change your primary email address.  If you have any questions, then please Write a Comment below!
    Access reports are powerful and flexible. Learn how to create a query and then a grouped report using the wizard. Modify the report design after the wizard is done to make it look better. There will be another video to explain how to put the final p…

    779 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

    11 Experts available now in Live!

    Get 1:1 Help Now