Skip to content

Instantly share code, notes, and snippets.

@chutchinson
Created June 15, 2020 16:36
Show Gist options
  • Save chutchinson/65b88249202298a55c0c5d94ec5a50d1 to your computer and use it in GitHub Desktop.
Save chutchinson/65b88249202298a55c0c5d94ec5a50d1 to your computer and use it in GitHub Desktop.
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