Asked
Viewed 167k times
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;
}
}
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.
13 Comments
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...
Explore related questions
See similar questions with these tags.

