Is a GUID unique 100% of the time?

2018-12-31 05:08发布

Is a GUID unique 100% of the time?

Will it stay unique over multiple threads?

19条回答
春风洒进眼中
2楼-- · 2018-12-31 05:47

None seems to mention the actual math of the probability of it occurring.

First, let's assume we can use the entire 128 bit space (Guid v4 only uses 122 bits).

We know that the general probability of NOT getting a duplicate in n picks is:

(1-1/2128)(1-2/2128)...(1-(n-1)/2128)

Because 2128 is much much larger than n, we can approximate this to:

(1-1/2128)n(n-1)/2

And because we can assume n is much much larger than 0, we can approximate that to:

(1-1/2128)n^2/2

Now we can equate this to the "acceptable" probability, let's say 1%:

(1-1/2128)n^2/2 = 0.01

Which we solve for n and get:

n = sqrt(2* log 0.01 / log (1-1/2128))

Which Wolfram Alpha gets to be 5.598318 × 1019

To put that number into perspective, lets take 10000 machines, each having a 4 core CPU, doing 4Ghz and spending 10000 cycles to generate a Guid and doing nothing else. It would then take ~111 years before they generate a duplicate.

查看更多
登录 后发表回答