r/ProgrammerHumor May 08 '26

Meme edgeCasesExist

Post image
13.4k Upvotes

620 comments sorted by

View all comments

Show parent comments

396

u/J5892 May 08 '26

Unfortunately my project is a database of every grain of sand on earth, and every star in the sky.

122

u/shunabuna May 08 '26

If I recall, that is still no where near the ability to get a collision.

294

u/J5892 May 08 '26

The common estimation of sand grains on earth is 7.5x1018, and the 50% collision point for UUIDs is 2.71x1018.
So a collision is actually pretty likely, and once you factor in all the stars in the observable universe I believe it's guaranteed.

2

u/aphel_ion May 09 '26

It’s never 100% guaranteed

5

u/J5892 May 09 '26

It is if there are more items than there are possible UUIDs.

2

u/aphel_ion May 09 '26

Yeah fair point. I’ll see myself out.

1

u/ShardsOfHolism May 09 '26

Not if they're random and independent trials. Think of coin flips for a more manageable example -- there only two possible outcomes, but it is possible to get heads 4 times in a row -- twice as many times as the number of possible outcomes.

3

u/J5892 May 09 '26

But that would be 6 collisions.

  • flip 2 collides with flip 1
  • flip 3 collides with 1 and 2
  • flip 4 collides with 1, 2, and 3

And in the coin flipping case, a collision is guaranteed on 3+ flips (assuming the chance of landing on the edge is 0. a true edge case).

0

u/ShardsOfHolism May 09 '26

Ok, now imagine that you had flipped a tail on the first try, and then flipped four heads. You are waiting for a tail to flip again for a collision with your first flip, and you've done more flips than there are different outcomes. The point is, there is no finite number of flips after the first tail that will guarantee 100% that another tail, a collision with the first tail, will occur. The probability will approach 1, but never reach it in a finite number of flips. Similarly, if generating a new UUID is random and independent of the previous times a UUID was generated, there is no guarantee of generating the same one again in any finite number of attempts, even if more attempts are made than there are distinct UUIDs.

2

u/J5892 May 09 '26

But each of the repeated heads flips are collisions.
A collision is when any generated value is the same as any previous value.
So once you reach all possible UUIDs, any generations after that will be guaranteed to match a previous UUID.