如何从Java UUID生成唯一的36位数字(ONLY NUMBER)



当我们需要36个字符的通用唯一数作为alfa数字,但看不到生成36位数字(唯一(的真实函数时,使用UUID.randomUUID((是最好的。

String lUUID = String.format("%040d", new BigInteger(UUID.randomUUID().toString().replace("-", ""), 16));

上面的代码我们可以用来生成唯一的数字,但它给出了40位数字,并且不能保证是唯一的。

您绝对不能像UUID.randomUUID()那样保证的唯一性。如果不与某个地方的服务器同步,你就无法保证这一点;你可以让它变得不可能。

但是,为了使它尽可能不可能,只需使用随机构造函数:

SecureRandom random = new SecureRandom()
BigInteger bigint;
do {
bigint = new BigInteger(120, random); // 2^120 > 10^36
} while (bigint.compareTo(BigInteger.TEN.pow(36)) >= 0);

最新更新