Created
January 9, 2025 15:07
-
-
Save ELI7VH/4d72db07ffafbc3788bbee524aba2897 to your computer and use it in GitHub Desktop.
stolen from: https://stackoverflow.com/a/9543797/2213321
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 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(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment