Reputation: 121
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.
Upvotes: 12
Views: 3497
Reputation: 3944
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.
Upvotes: 6
Reputation: 81429
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.
Upvotes: 6
Reputation: 1666
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.
Upvotes: 3
Reputation: 151
Social MSDN gives little info, but doesn't answer your question whether a collision is more likely or not. Guid Structure tells a GUID is not a string but "A GUID is a 128-bit integer (16 bytes) that can be used across all computers and networks wherever a unique identifier is required. Such an identifier has a very low probability of being duplicated."
Upvotes: 0
Reputation: 7484
I would suggest to use actual guid's. The chances that your random string generator would be unique is far less than that of a guid.
Upvotes: 1
Reputation: 391
It depends on algorithm which you will use. If you have good generator the result would be the same.
The likelihood depends on how good both the generators are (yours vs. GUID one).
Upvotes: 1