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

大城市里の小女人 提交于 2019-11-27 14:20:15

问题


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

回答1:


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.



来源:https://stackoverflow.com/questions/24876188/how-big-is-the-chance-to-get-a-java-uuid-randomuuid-collision

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!