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;
    }
}

asked Jul 21, 2014 at 22:55

daniels's user avatar

4

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.

answered Jul 21, 2014 at 23:01

Óscar López's user avatar

13 Comments

So you're saying there's a chance?

@Jack yes, but it’s astronomically low.

Ah meant it more a as a joke... thank you for your well described answer!

I foresee an eventual explosion of human and human-like civilization across the galaxy and beyond which coincides with an equally exponential increase in the number of UUIDs each person creates, such that when the population is in the quadrillions, this will eventually become a problem similar to Y2k and it will cause everything to grind to a ha.... wait a second...

@ValerijDobler more than likely you found a bug in your code, not a uuid collision :)

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.