views:

80

answers:

1

I've seen many situations before where a PK is a Guid with no default value, and as such the developer would generate random Guids and use them to insert into the database.

I've often wondered what is the possibility that they would generate a number that already existed?

+4  A: 

Some Information: The number of values 16 bytes can hold is 2^128.

Total = number of total values that can be represented

Actual = number of values that have already been used

The probability of inserting an ID into a table that already exists could be formulated as the following:

1 - (Total - Actual) / Total

I've done some preliminary calculations and I've come up with the following:

Say you had a table that had 10 million records already in the table, then the probability would be

1 - (2 ^ 128 - 10,000,000) / 2 ^ 128

which is appromixately

0.00000000000000000000000000000003

I think now I know why it's said that:

"While each generated GUID is not guaranteed to be unique, the total number of unique keys is so large that the probability of the same number being generated twice is infinitesimally small"

from the wiki

Joseph
That's safe enough for me in most cases.
Fredrik Mörk
You mean 16 BYTES, not 16 BITS.
Michael Kohne
It should also be noted that some GUID generation schemes include time/date and/or ethernet mac address information. These schemes have bad effects (you can use the GUID to trace the generator and GUIDs become more predictable) and good effects (since mac addresses are unique and time/date is moving always forward, you have even less chance of multiple sources generating the same GUID).
Michael Kohne
@Michael thanks for the catch, I completely missed that. Editted.
Joseph
@Michael yeah I'm aware that some generation schemes are considered predictable, but in this case I'm speaking purely of the uniqueness factor, and not so much as to whether or not someone can determine or predict future values of the database.
Joseph