Last active
May 23, 2016 23:38
-
-
Save syamsathyan/1f813eb7e78119f21c2f3d474b47b90d to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public static class BaseKeyGenerator | |
{ | |
public static void TestBaseKeyGenerator() | |
{ | |
// create five IDs of six, base 62 characters | |
for (int i=0; i<5; i++) Console.WriteLine(BaseKeyGenerator.GetBase62(6)); | |
// create five IDs of eight base 36 characters | |
for (int i=0; i<5; i++) Console.WriteLine(BaseKeyGenerator.GetBase36(8)); | |
} | |
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(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment