Created
July 24, 2017 08:02
-
-
Save rqx110/6458b5b0b6c2ff244a3e9f8897ac8c2e to your computer and use it in GitHub Desktop.
Base 62 ID generator
This file contains hidden or 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 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(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
look at https://stackoverflow.com/a/9543797