Is a GUID unique 100% of the time?

前端 未结 22 2829
孤独总比滥情好
孤独总比滥情好 2020-11-22 01:31

Is a GUID unique 100% of the time?

Will it stay unique over multiple threads?

22条回答
  •  独厮守ぢ
    2020-11-22 02:15

    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.

提交回复
热议问题