How big is the chance to get a Java UUID.randomUUID collision?

JavaUuid

Java Problem Overview


I need to create some uniques files in Java and i plan to use UUID.randomUUID to generate their names. Is there any chance to get a collision for this? Should i do something like bellow os I shouldn't worry about this?

Integer attemptsToGenerateUUID = 1;

while (true) {
    UUID fileUUID = UUID.randomUUID();

    if (fileDoesNotExistwith this UUID name) {
        save file;
        break;
    }

    attemptsToGenerateUUID += 1;

    if (attemptsToGenerateUUID > 64) {
        return false;
    }
}

Java Solutions


Solution 1 - Java

According to wikipedia, regarding the probability of duplicates in random UUIDs:

> Only after generating 1 billion UUIDs every second for the next 100 years, the probability of creating just one duplicate would be about 50%. Or, to put it another way, the probability of one duplicate would be about 50% if every person on earth owned 600 million UUIDs.

I guess the same reasoning applies to Java's implementation of UUID. So no, you should not worry about this.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestiondanielsView Question on Stackoverflow
Solution 1 - JavaÓscar LópezView Answer on Stackoverflow