在 Java 中使用 UUID 的最高有效位发生冲突的可能性 [英] Likelihood of collision using most significant bits of a UUID in Java

查看:51
本文介绍了在 Java 中使用 UUID 的最高有效位发生冲突的可能性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果我使用 Long uuid = UUID.randomUUID().getMostSignificantBits() 发生碰撞的可能性有多大.它会切断最低有效位,因此您可能会遇到冲突,对吗?

If I'm using Long uuid = UUID.randomUUID().getMostSignificantBits() how likely is it to get a collision. It cuts off the least significant bits, so there is a possibility that you run into a collision, right?

推荐答案

根据 文档,静态方法 UUID.randomUUID() 生成类型 4 UUID.

According to the documentation, the static method UUID.randomUUID() generates a type 4 UUID.

这意味着 6 位用于某些类型信息,其余 122 位是随机分配的.

This means that six bits are used for some type information and the remaining 122 bits are assigned randomly.

这六个非随机位分布在 UUID 的最高有效半部分中的四个和最低有效半部分中的两个.因此,您的 UUID 的最重要的一半包含 60 位随机性,这意味着您平均需要生成 2^30 个 UUID 才能发生碰撞(相比之下,完整的 UUID 为 2^61).

The six non-random bits are distributed with four in the most significant half of the UUID and two in the least significant half. So the most significant half of your UUID contains 60 bits of randomness, which means you on average need to generate 2^30 UUIDs to get a collision (compared to 2^61 for the full UUID).

所以我会说你很安全.但是请注意,正如 Carl Seleborg 提到的那样,这对于其他类型的 UUID 绝对不是真的.

So I would say that you are rather safe. Note, however that this is absolutely not true for other types of UUIDs, as Carl Seleborg mentions.

顺便说一句,使用 UUID 的最低有效一半(或仅使用 SecureRandom 生成随机长)会稍微好一点.

Incidentally, you would be slightly better off by using the least significant half of the UUID (or just generating a random long using SecureRandom).

这篇关于在 Java 中使用 UUID 的最高有效位发生冲突的可能性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆