Kat Lim Ruiz
Kat Lim Ruiz

Reputation: 2562

Can machine-generated UUID produce only zeroes?

Will UUID (or GUID) algorithms ever generate an "artificial" number?

Such as:

I want to know if it is safe to set a GUID manually as a PK for a table that generates its own PK (NEWID or NEWSEQUENTIALID in TSQL).

Upvotes: 0

Views: 270

Answers (3)

mcdowella
mcdowella

Reputation: 19621

The format of many sorts of GUID is specified in http://en.wikipedia.org/wiki/Universally_unique_identifier and http://www.ietf.org/rfc/rfc4122.txt, and it is likely that an arbitrary choice will not conflict with any properly created GUID in any case.

It is however possible that an arbitrary GUID that you choose could be the same as an arbitrary GUID that somebody else chooses - as you are both breaking the rules. To avoid this, I suggest that you get hold of a properly created GUID of your own and either put it in as a config setting or hardwire it into your your code. A command line program to generate a GUID called uuidgen exists on linux and comes with Windows SDKs. A quick search suggests that you can also generate one via web pages such as http://www.famkruithof.net/uuid/uuidgen and https://www.guidgenerator.com/online-guid-generator.aspx.

Here are some freshly generated GUIDs anyway

{CB58921C-66A9-4C64-B2F4-413166736071}

{8FE41A9D-A0AC-4D32-8C10-48796DEFBD9C}

{E280FE84-0E60-4880-91DC-7A7BB886E1EE}

(From a script I found at http://www.somacon.com/p113.php).

Upvotes: 1

Brian Driscoll
Brian Driscoll

Reputation: 19635

The typical algorithm for generating a GUID on a Windows PC is to concatenate two hex encoded values, usually a portion of the MAC address and the current timestamp (so all GUIDs generated by the same PC will differ only by the timestamp portion). So, in your case it would be nearly impossible for the PC to generate your sample values randomly.

Upvotes: -1

Sergey Kalinichenko
Sergey Kalinichenko

Reputation: 727077

The probability of generating any specific GUID is 2-128, so no matter what GUIDs you set as PKs, the chances of generating a duplicate are tiny. How tiny? Your chances of winning NY lottery are about one nonillion , or 1030, higher than generating a duplicate GUID, so you are pretty safe.

When you need to use fixed IDs, it is common practice to generate GUIDs on your computer, say, through the Visual Studio menu, or by writing a tiny program, and then hard-coding that GUID into your program as a "well-known value".

Upvotes: 1

Related Questions