使用Boost库与Java生成的UUID的唯一性 [英] The uniqueness of UUID generated using Boost library vs Java

查看:418
本文介绍了使用Boost库与Java生成的UUID的唯一性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我组织中的Android应用程序在首次启动该应用程序时需要为每个用户分配一个UUID(版本4).目前,我们为此目的使用Boost库1.58.0,我们的Android应用程序将使用JNI运行下面的代码用于生成UUIDv4:

An Android app from my organization needs to assign each users an UUID(version 4) when they first launch the app, currently we're using Boost library 1.58.0 for this purpose, our Android app will use JNI to run the code below for generating UUIDv4:

boost::uuids::basic_random_generator<boost::random::lagged_fibonacci44497> generator;
generator(); //generating UUIDv4

该代码可以正常工作多年,现在我们决定用Java中的API替换它(

The code worked fine for years, now we're deciding to replace it with an API in Java(UUID.randomUUID()). But we think it would be better if we can know about the uniqueness of these two ways before making the change.

我的问题:

  1. 使用 boost :: random :: lagged_fibonacci44497 是否会减少使用我们的应用程序具有相同UUID(冲突)的两个设备的机会?
  2. 如果是,我们能知道发生碰撞的可能性吗?
  3. 该代码是否比UUID.randomUUID()是否具有唯一性?
  1. Does the using of boost::random::lagged_fibonacci44497 reduce the chance of two devices using our app having the same UUID(collision)?
  2. If it is, can we know the probability of collision?
  3. Is that code better than UUID.randomUUID() in terms of uniqueness?

推荐答案

java.util.randomUUID()的文档说,它返回的UUID是使用密码学强的伪随机数生成的"发电机".因此,每个这样的UUID几乎可以肯定是唯一的".出于大多数目的(至少在4.3之后的Android版本中;请参见此问题).

The documentation for java.util.randomUUID() says the UUIDs it returns are "generated using a cryptographically strong pseudo random number generator". Thus, each such UUID will be almost certainly "unique" for most purposes (at least in Android versions after 4.3; see this question).

第4版UUID由122个随机选择的位组成,因此最多有2 ^ 122个此类UUID.但是,不能保证随机选择的有限长度值本身是唯一的.

A version 4 UUID consists of 122 randomly chosen bits, so that there are at most 2^122 UUIDs of this kind. However, no randomly chosen value of finite length is guaranteed to be unique by itself.

粗略地说,当随机生成第4版UUID时,仅在生成约2 ^ 61个UUID(即约27亿)后,偶然碰撞的机会才变得不可忽略(请参阅

Roughly speaking, when version 4 UUIDs are generated at random, the chance of accidental collision becomes non-negligible only after about 2^61 UUIDs are generated, which is about 2.7 billion billion (see "Birthday problem" for a precise statement and formulas).

这篇关于使用Boost库与Java生成的UUID的唯一性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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