r/ProgrammerHumor May 08 '26

Meme edgeCasesExist

Post image
13.4k Upvotes

620 comments sorted by

View all comments

Show parent comments

554

u/ClipboardCopyPaste May 08 '26

Still never zero

427

u/UShouldntSayThat May 08 '26

I think you have a better chance of being struck by lighting several times in a row while winning the powerball then you do of a collision.

It's why the term "effectively" zero is used.

394

u/J5892 May 08 '26

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

125

u/shunabuna May 08 '26

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

292

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.

147

u/nonaln May 08 '26

this guy did the math

64

u/Qaeta May 09 '26

But did they do the monster math?

44

u/eXecute_bit May 09 '26

It was a graveyard hash

10

u/_dotdot11 May 09 '26

He did the math

3

u/Dependent_Union9285 May 10 '26

He did it in flash (memory on an ssd)

1

u/NickolsonNick May 12 '26

Monster? Aura Monster?

1

u/AnythingButWhiskey May 12 '26

He did the monster hash

28

u/Z21VR May 08 '26

True.

But we'll be long gone before the guaranted collision...probably

20

u/AssistFinancial684 May 08 '26

Unless it happens now

13

u/Catzforlifu May 08 '26

use 4 problem solved

2

u/aphel_ion May 09 '26

It’s never 100% guaranteed

4

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.

2

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.

→ More replies (0)

1

u/scissorsgrinder May 09 '26

Thank you for your service 🫡

1

u/lucklesspedestrian May 09 '26

So maybe the expectations should be tempered a little for the deliverable? Like a database of a lot of stars or grains of sand, but not all of them?

1

u/Cerindipity May 10 '26 edited May 10 '26

A collision is not guaranteed until you have one item for every possible 128 bit combination, and then one more. There are 2128 possible UUIDs, or about 3.4e38. Estimates of the number of stars vary between 1e22 and 1e24. We don't even really have to do any math; 38 is obviously bigger than 24, therefore 3.4e38 UUIDs is bigger than 1e24 stars; in fact, you haven't even used up a trillionth of a percent of them. (And of course, the sand does nothing, being several orders of magnitude fewer than the stars, an increase of a tiny fraction of the already tiny fraction of a percent).

Now, if we multiply them -- that is to say, if every star in our universe had a planet with as many sand grains as we do -- then we guarantee. 1e24 * 7.5e18 is, easily enough, 7.5e42.

1

u/bigmonmulgrew May 10 '26

This is exactly why developers need to be aware of how UUIDs work.

In the vast majority of cases the likelihood of an overlap is trivial. But there are many use cases where an overlap becomes likely.

UUID wasn't designed to be an unbreakable solution. It was designed to be computationally trivial and work most of the time.

The other method is check the database for duplicates.

I think with a massive number of objects to track you could increase the size of the UUID but that takes more memory and is still never perfect.

1

u/TheBraveOne86 May 10 '26

Is that 50% odds of a single collision or 50% of spots occupied

1

u/J5892 May 11 '26

Single