获得Java UUID.randomUUID冲突的机会有多大? [英] How big is the chance to get a Java UUID.randomUUID collision?
问题描述
我需要在Java中创建一些唯一文件,我计划使用UUID.randomUUID来生成它们的名称。有没有机会为此发生碰撞?我应该做一些像bellow os我不应该担心这个吗?
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;
}
}
推荐答案
根据维基百科,关于随机UUID中重复的可能性:
According to wikipedia, regarding the probability of duplicates in random UUIDs:
只有在接下来的100年中每秒产生10亿UUID后,创建一个副本的概率大约为50%。或者,换句话说,如果地球上的每个人拥有6亿UUID,则一次重复的概率约为50%。
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.
我想同样的推理也适用于Java的UUID实现。所以不,你不应该担心这一点。
I guess the same reasoning applies to Java's implementation of UUID. So no, you should not worry about this.
这篇关于获得Java UUID.randomUUID冲突的机会有多大?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!