How do I generate a Unique AlphaNumeric ID in C#
-
i have tried Guid.NewGuid().ToString().GetHashCode().ToString("x").ToUpper(); but I do not get Unique Codes after 10,000 tries i need to add ABCDEFGHIJKLMNOPQRSTUVWXYZ and 1234567890 somebody help!!!
-
i have tried Guid.NewGuid().ToString().GetHashCode().ToString("x").ToUpper(); but I do not get Unique Codes after 10,000 tries i need to add ABCDEFGHIJKLMNOPQRSTUVWXYZ and 1234567890 somebody help!!!
How many characters do you need? There are a number of random password generators available, have you looked? One of those should be sufficient.
No comment
-
i have tried Guid.NewGuid().ToString().GetHashCode().ToString("x").ToUpper(); but I do not get Unique Codes after 10,000 tries i need to add ABCDEFGHIJKLMNOPQRSTUVWXYZ and 1234567890 somebody help!!!
-
i have tried Guid.NewGuid().ToString().GetHashCode().ToString("x").ToUpper(); but I do not get Unique Codes after 10,000 tries i need to add ABCDEFGHIJKLMNOPQRSTUVWXYZ and 1234567890 somebody help!!!
GUIDs are large enough that they'll usually be unique (But not guaranteed)... But once you take the hash code, you're dropping from a 16-byte GUID to a 4-byte integer, and limiting it even more by the hashing algorithm... So why not build your own algorithm? If you just need them to be unique, you can start from "AAAA" (Or whatever length) and just increase as if it was a number... AAAA, AAAB, AAAC, ..., AAAZ, AAA0, AAA1, and so on. If you want them to look random and be unpredictable, just generate a number of random integers from 0 to 36, and convert each one to a letter or digit... String them together, and you've got a random ID... Then just keep a hash/dictionary of previous IDs to compare against, and re-generate if you create a duplicate.
Proud to have finally moved to the A-Ark. Which one are you in?
Author of the Guardians Saga (Sci-Fi/Fantasy novels) -
i have tried Guid.NewGuid().ToString().GetHashCode().ToString("x").ToUpper(); but I do not get Unique Codes after 10,000 tries i need to add ABCDEFGHIJKLMNOPQRSTUVWXYZ and 1234567890 somebody help!!!
Guid should already satisfy your requirement in creating unique identifiers. They're already a product of cryptographic hashing functions, so you shouldn't need to hash them again. You can use ToString with a format provider to get a string representation of the value without the dashes. Or, as an alternative, you can always roll your own UUID implementation according to the standards. One other note - GetHashCode isn't a cryptographic hash, nor is it guaranteed to be consistent between frameworks or platforms. Check the Remarks section at MSDN.