This project is read-only.

The substring of GUID is not unique

Jul 23, 2012 at 7:24 AM
Edited Jul 23, 2012 at 7:26 AM

In this project, the random string is generated by  following code:

Guid newguid = Guid.NewGuid();

string randomStr =            newguid.ToString("N");

string tracStr = randomStr.Substring(0,            (int)keyLength);

GUID is a unique sting, but the substing of GUID is not always unique. 

http://blogs.msdn.com/b/oldnewthing/archive/2008/06/27/8659071.aspx

Jul 23, 2012 at 10:03 AM

I discoveried another issue. The key generated by substring of Guid only contains 0~9 and A~F.

I changed the algorithm which from the stackoverflow. 

http://stackoverflow.com/questions/1344221/how-can-i-generate-random-8-character-alphanumeric-strings-in-c

var chars = "ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
var random = new Random();
var result = new string(
   
Enumerable.Repeat(chars, 8)
             
.Select(s => s[random.Next(s.Length)])
             
.ToArray());