Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Which part of a GUID is most worth keeping?

I need to generate a unique ID and was considering Guid.NewGuid to do this, which generates something of the form:

0fe66778-c4a8-4f93-9bda-366224df6f11

This is a little long for the string-type database column that it will end up residing in, so I was planning on truncating it.

The question is: Is one end of a GUID more preferable than the rest in terms of uniqueness? Should I be lopping off the start, the end, or removing parts from the middle? Or does it just not matter?

like image 447
izb Avatar asked Oct 31 '11 16:10

izb


People also ask

What makes a GUID unique?

How unique is unique? A GUID is a unique number that can be used as an identifier for anything in the universe, but unlike ISBN there is no central authority - the uniqueness of a GUID relies on the algorthm that was used to generate it.

What are the odds of a GUID repeating?

How unique is a GUID? 128-bits is big enough and the generation algorithm is unique enough that if 1,000,000,000 GUIDs per second were generated for 1 year the probability of a duplicate would be only 50%. Or if every human on Earth generated 600,000,000 GUIDs there would only be a 50% probability of a duplicate.

Can GUID be duplicated?

It's possible to generate an identical guid over and over. However, the chances of it happening are so low that you can assume they are unique.

How many GUIDs are possible?

Question: How many GUID combinations are there? Answer: There are 122 random bits (128 – 2 for variant – 4 for version) so this calculates to 2^122 or 5,316,911,983,139,663,491,615,228,241,121,400,000 possible combinations.


1 Answers

You can save space by using a base64 string instead:

var g = Guid.NewGuid();
var s = Convert.ToBase64String(g.ToByteArray());

Console.WriteLine(g);
Console.WriteLine(s);

This will save you 12 characters (8 if you weren't using the hyphens).

like image 56
Austin Salonen Avatar answered Oct 14 '22 13:10

Austin Salonen