获得 Java UUID.randomUUID 冲突的机会有多大?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/24876188/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-14 15:19:25  来源:igfitidea点击:

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

javauuid

提问by daniels

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?

我需要在 Java 中创建一些唯一文件,我计划使用 UUID.randomUUID 来生成它们的名称。有没有机会为此发生碰撞?我应该做类似波纹管的事情吗,我不应该担心这个?

Integer attemptsToGenerateUUID = 1;

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

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

    attemptsToGenerateUUID += 1;

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

采纳答案by óscar López

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

根据维基百科,关于随机 UUID 中重复的概率:

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.

只有在接下来的 100 年中每秒生成 10 亿个 UUID 之后,仅创建一个重复项的概率约为 50%。或者,换句话说,如果地球上每个人都拥有 6 亿个 UUID,一个重复的概率约为 50%。

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

我想同样的推理也适用于 Java 的 UUID 实现。所以不,你不应该担心这个。