Created
June 15, 2020 16:36
-
-
Save chutchinson/65b88249202298a55c0c5d94ec5a50d1 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 string GenerateId(int length) | |
{ | |
const string symbols = "abcdefghijklmnopqrstuvwxyz0123456789"; | |
byte[] buffer = null; | |
try | |
{ | |
buffer = ArrayPool<byte>.Shared.Rent(length); | |
for (var i = 0; i < length; i++) | |
{ | |
var index = RandomNumberGenerator.GetInt32(length); | |
buffer[i] = (byte)symbols[index]; | |
} | |
return Encoding.ASCII.GetString(buffer, 0, length); | |
} | |
finally | |
{ | |
ArrayPool<byte>.Shared.Return(buffer); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment