Base 62 is used by tinyurl and bit.ly for shortened URLs. This is a well-understood method for creating "unique", readable identifiers. Of course, you will need to save the created identifiers and check for duplicates when creating to ensure uniqueness. (see code at the bottom of the answer)
Base uniqueness indicators 62
5 characters in base 62 will give you 62 ^ 5 unique identifiers = 916,132,832 (~ 1 billion) With 10k ID per day, you'll be fine for 91k + days
6 characters in base 62 will give you 62 ^ 6 unique identifiers = 56 800 235 584 (56+ billion) With 10,000 IDs per day, you will be in order for 5+ million days.
Base 36 uniqueness indicators
6 characters will give you 36 ^ 6 unique identifiers = 2,176,782,336 (2+ billion)
7 characters will give you 36 ^ 7 unique identifiers = 78 364 164 000 (78+ billion)
The 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
Paul Sasik Mar 03 2018-12-12T00: 00Z
source share