发生 Java UUID.Random UUID 冲突的几率有多大?

我需要在 Java 中创建一些惟一的文件,我计划使用 UUID.Random UUID 来生成它们的名称。有可能因为这个撞车吗?我应该做一些类似于风箱或我不应该担心这个?

Integer attemptsToGenerateUUID = 1;


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


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


attemptsToGenerateUUID += 1;


if (attemptsToGenerateUUID > 64) {
return false;
}
}
89450 次浏览

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.