Here is the code for my random string generator
private static string GetUniqueKey()
{
int maxSize = 15 ;
char[] chars = new char[62];
string a = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890";
chars = a.ToCharArray();
int size = maxSize ;
byte[] data = new byte[1];
RNGCryptoServiceProvider crypto = new RNGCryptoServiceProvider();
crypto.GetNonZeroBytes(data) ;
size = maxSize ;
data = new byte[size];
crypto.GetNonZeroBytes(data);
StringBuilder result = new StringBuilder(size) ;
foreach(byte b in data )
{
result.Append(chars[b % (chars.Length - 1)]);
}
return result.ToString();
}
How do I write a unit test that would test for 100% guarantee that no 2 threads will generate the same random number
I don't want to lock thread because it creates performance night mare under extreme load tests.
This logic will be used in load balanced 8 app servers.
I can't use GUID because this random string should be humanly readable like a credit card number.
Do I have to constantly read database to ensure that this is a unique number before I store in DB?
Aucun commentaire:
Enregistrer un commentaire