GUID, 30 character random string

I need to create a unique string 30 characters long. At first, I was going to generate a GUID and just delete the first two characters.

Guid.NewGuid().ToString("N").Substring(2); 

Will the removal of the first two characters significantly affect the "uniqueness"? Is this something I should worry about?

Is there a better way to generate a random 30 character string that is guaranteed to be unique?

+7
guid
source share
3 answers

Removing two hexadecimal characters or the equivalent 8 bits from the GUID will make it less unique, but 120 bits still create a pretty good unique value. If you do not want to generate millions of identifiers every second, it should be safe to remove some bits from the timestamp and uniquifier without risking collision. See, for example, Wikipedia for the GUID structure .

An alternative solution would be Base64's GUID encoding or something like that if you are not limited to hexadecimal characters only. The 128 bits encoded in Base64 give a string of length 24. Then you can even add 6 more random characters to superimpose the string with 30 characters, making it even more unique.

+5
source share

GUID truncation is uniqueness. To understand why you need to understand how a GUID is created. It consists of several parts:

  • 60 bit timestamp
  • 48 bit computer id
  • 14 bit uniquifier
  • 6 bit fixed

Discarding the first two characters, you discard the 8 most significant bits of the timestamp part. This article explains this well and the dangers of truncating a GUID. It also explains how you can use the same technique as in the GUID to create unique identifiers that are not globally unique, but that will be unique for more limited conditions.

+5
source share

As the other defendants in front of me said, if you simply delete two characters from the GUID, then it will no longer be unique.

But there is another way: you can reduce the GUID to 20 characters without losing information or uniqueness using ASCII encoding.

Mark this blog post by Jeff Atwood:
Horror Coding: Equipping our ASCII Armor

+1
source share

All Articles