Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Generating human-readable/usable, short but unique IDs

  • Need to handle > 1000 but < 10000 new records per day

  • Cannot use GUID/UUIDs, auto increment numbers etc.

  • Ideally should be 5 or 6 chars long, can be alpha of course

  • Would like to reuse existing, well-known algos, if available

Anything out there ?

like image 975
Kumar Avatar asked Mar 03 '12 05:03

Kumar


People also ask

What is unique ID generation?

The Unique ID shall be used at the time of admission into various Govt./Provincialised Colleges and State Universities under Higher Education, Assam (General) One student can generate only 1 (One) unique ID and that Unique ID shall be used in all applications for admission into colleges/universities.

Is there a shorter UUID?

1. NanoID is Only 108 bytes in Size. Unlike UUID, NanoID is 4.5 times smaller in size and does not have any dependencies.

How do I create a unique ID in Excel?

You can generate a unique value using a formula in the spreadsheet. An ID must be a value, not a formula, though, so copy (Ctrl+C) and paste as plain text (Shift+Ctrl+V) the result of the formula calculation into the cell meant to contain the new ID. That's all there is to it!

What is UUID generator?

A UUID (Universal Unique Identifier) is a 128-bit value used to uniquely identify an object or entity on the internet. Depending on the specific mechanisms used, a UUID is either guaranteed to be different or is, at least, extremely likely to be different from any other UUID generated until A.D. 3400.


2 Answers

Base 62 is used by tinyurl and bit.ly for the abbreviated URLs. It's a well-understood method for creating "unique", human-readable IDs. Of course you will have to store the created IDs and check for duplicates on creation to ensure uniqueness. (See code at bottom of answer)

Base 62 uniqueness metrics

5 chars in base 62 will give you 62^5 unique IDs = 916,132,832 (~1 billion) At 10k IDs per day you will be ok for 91k+ days

6 chars in base 62 will give you 62^6 unique IDs = 56,800,235,584 (56+ billion) At 10k IDs per day you will be ok for 5+ million days

Base 36 uniqueness metrics

6 chars will give you 36^6 unique IDs = 2,176,782,336 (2+ billion)

7 chars will give you 36^7 unique IDs = 78,364,164,096 (78+ billion)

Code:

public void TestRandomIdGenerator() {     // create five IDs of six, base 62 characters     for (int i=0; i<5; i++) Console.WriteLine(RandomIdGenerator.GetBase62(6));      // create five IDs of eight base 36 characters     for (int i=0; i<5; i++) Console.WriteLine(RandomIdGenerator.GetBase36(8)); }  public static class RandomIdGenerator  {     private static char[] _base62chars =          "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz"         .ToCharArray();      private static Random _random = new Random();      public static string GetBase62(int length)      {         var sb = new StringBuilder(length);          for (int i=0; i<length; i++)              sb.Append(_base62chars[_random.Next(62)]);          return sb.ToString();     }             public static string GetBase36(int length)      {         var sb = new StringBuilder(length);          for (int i=0; i<length; i++)              sb.Append(_base62chars[_random.Next(36)]);          return sb.ToString();     } } 

Output:

 z5KyMg wd4SUp uSzQtH UPrGAT UIf2IS  QCF9GNM5 0UV3TFSS 3MG91VKP 7NTRF10T AJK3AJU7 
like image 190
Paul Sasik Avatar answered Oct 03 '22 07:10

Paul Sasik


I recommend http://hashids.org/ which converts any number (e.g. DB ID) into a string (using salt).

It allows decoding this string back to the number. So you don't need to store it in the database.

Has libs for JavaScript, Ruby, Python, Java, Scala, PHP, Perl, Swift, Clojure, Objective-C, C, C++11, Go, Erlang, Lua, Elixir, ColdFusion, Groovy, Kotlin, Nim, VBA, CoffeeScript and for Node.js & .NET.

like image 27
Slawa Avatar answered Oct 03 '22 05:10

Slawa