Registration Code Generation

I was recently assigned the task of creating unique registration codes for a product. The requirements were as follows:

  • 12 digits long
  • 3 groups of 4, separated by hyphens
  • No duplicates, of course
  • Easy for anyone to recognize and enter

I began by using the GUID.NewGuid() method. This gave me a long hexadecimal string of random characters between 0 and 9, a-f. By taking various substrings from this and then making sure there were no collisions with any existing registration code, I could have a very randomized registration code generator.

However, upon further reflection–and recalling difficulty I myself have had in the past trying to enter the right codes–I came up with the following issues:

  • It’s really difficult to distinguish between a zero and the letter “O”. The people trying to enter the registration code would not know that it was generated from a GUID that only had the letters A-F, so I wanted to eliminate this confusion for the user.
  • It’s really difficult to distinguish between the lowercase “L” -“l” and the number one (“1”) in some fonts.
  • You probably want to avoid some obvious combinations in the code like “KKK” or “666”. Even though our code would contain groups of 4 characters, it seemed best to still avoid those.

We opted to make the registration code all upper case. We also decided to change the ones and zeroes to “h’s” and “k’s. We skipped “g” since it goes “below the line” in the lower case version. We also skipped “i” since that can look like a lowercase “l” or a “1” if, for whatever reason, the printing isn’t clear. So, with all of that, here’s the code:

public string CreateRegistrationCode()
{            
    string code = "";
    while (code == "") //repeat until you don't get a collision
    {
         //replace zeroes and ones to eliminate confusion between o's and L's.  
         //Also get rid of the hyphens in the GUID
         string newGuidString = Guid.NewGuid().ToString().Replace("-", "").Replace("0", "h").Replace("1", "k");
         
         code = newGuidString.Substring(0, 4) 
                 + "-" + newGuidString.Substring(4, 4) 
                 + "-" + newGuidString.Substring(8, 4);
        
         code = code.ToUpper().Replace("KKK", "HHH").Replace("666", "777"); // just to avoid an undesirable code

         var codeExists = (from r in DataContext.Registrations
                                  where r.Code == code
                                  select r).Any(); //test to see if the registration code already exists.  

         if (codeExists)
               code = "";
    }

    return code; 
}
Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: