I need to generate a unique string that is 30 characters in length. At first, I was going to generate a GUID and just remove the first two characters.
Guid.NewGuid().ToString("N").Substring(2);
Will removing the two first characters have a significant effect on the “uniqueness”? Is it something that I should be worried about?
Is there a better way of generating a random 30 character string that will be guaranteed to be unique?
Removing two hexadecimal characters or equivalently 8 bits from a GUID will make it less unique but 120 bits still make a quite good unique value. If you don’t want to generate millions of ids every second it should be safe to remove some bits from the timestamp and uniquifier without risking a collision. See for example the Wikipedia for the structure of GUIDs.
An alternative solution would be to encode the GUID in Base64 or something like that if you are not constraint to hexadecimal characters only. 128 bits encoded in Base64 yield a string of length 24. Then you can even add another 6 random characters to pad the string to 30 characters making it even more unique.