Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Guid vs random string

Tags:

guid

random

char

If I randomly generate a string of 32 characters-long can I use this string as a GUID for all intents and purposes?

Will the "GUID" I generate have more or less likelihood of collision than a "real" GUID?

Any more specific info on GUIDs and how they compare to random strings is appreciated.

like image 324
guidlerner Avatar asked May 18 '11 12:05

guidlerner


3 Answers

GUID-generation algorithms take into account the date and time as well as generating random numbers to create the final 128 bit value.

If you simply generate random strings w/o any other algorithmics thrown in then yes, you will run a much greater risk of collision. (Computers cannot create truly random numbers so other data has to folded into the GUID gen algorithms to lower risk of collision. GUID v1 for example used a computer's MAC address though that approach has been deprecated since it identifies the generating computer.)

You could create your own GUID value but why reinvent something that already works well?

Also, see Eric Lippert's answer as to why using a GUID is superior to using your own, home-brewed random ID generator.

like image 142
Paul Sasik Avatar answered Oct 10 '22 05:10

Paul Sasik


A GUID is not a 32-character long string. So no, you cannot use it in place of a GUID.

Depending on the encoding, a char can be either one or two bytes, so 32 chars can be 32 bytes or 64 bytes. A GUID is 16 bytes. If you have an equivalent amount of randomness in your generator, your string will produce less chance of collision. Saying that, the chance of collision in 16 bytes is pretty unlikely as it is.

The clinch is that you have to have at least as good a generator as the Guid generator to make it worthwhile. When you do that, patent it.

like image 39
Xhalent Avatar answered Oct 10 '22 05:10

Xhalent


Depends on the GUID you're comparing it against: nowadays most GUIDs are "Version 4", which is really just a big random number with some wasted bits. So as long as your random number generator is as good as the one used to generate the GUID, your solution is more unique.

If it's a Version 1 GUID, then it's probably more unique than a random number (assuming it's being used as expected: the system clock isn't being reset very often, the system has a network card, and the MAC address hasn't been tampered with) but most people don't use version 1 anymore because it leaks your MAC address.

like image 41
Rick Yorgason Avatar answered Oct 10 '22 03:10

Rick Yorgason