在JavaScript中生成UUID时发生冲突 [英] Collisions when generating UUIDs in JavaScript

查看:69
本文介绍了在JavaScript中生成UUID时发生冲突的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这与此问题有关.我正在使用以下此答案中的代码以在JavaScript中生成UUID:

This relates to this question. I am using the code below from this answer to generate a UUID in JavaScript:

'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'.replace(/[xy]/g, function(c) {
    var r = Math.random()*16|0, v = c == 'x' ? r : (r&0x3|0x8);
    return v.toString(16);
});

此解决方案似乎运行良好,但发生冲突.这就是我所拥有的:

This solution appeared to be working fine, but I am getting collisions. Here's what I have:

  • 在Google Chrome浏览器中运行的网络应用程序.
  • 16个用户.
  • 这些用户在过去两个月中已生成
  • 大约4000个UUID.
  • 我发生了大约20次碰撞-例如,今天生成的新UUID与大约两个月前相同(不同的用户).
  • A web application running in Google Chrome.
  • 16 users.
  • about 4000 UUIDs have been generated in the past two months by these users.
  • I got about 20 collisions - e.g., a new UUID generated today was the same as about two months ago (different user).

是什么原因导致此问题,又该如何避免呢?

What is causing this issue and how can I avoid it?

推荐答案

我的最佳猜测是Math.random()由于某种原因在系统上损坏了(听起来有点怪异).这是我见过的第一个关于任何人发生碰撞的报告.

My best guess is that Math.random() is broken on your system for some reason (bizarre as that sounds). This is the first report I've seen of anyone getting collisions.

node-uuid具有一个测试工具,您可以用来测试十六进制的分布该代码中的数字.如果一切正常,则不是Math.random(),因此请尝试将您正在使用的UUID实现替换为那里的uuid()方法,以查看是否仍然获得良好的结果.

node-uuid has a test harness that you can use to test the distribution of hex digits in that code. If that looks okay then it's not Math.random(), so then try substituting the UUID implementation you're using into the uuid() method there and see if you still get good results.

[更新:刚看到 Veselin的报告有关启动时出现Math.random()错误的信息.由于问题仅在启动时出现,因此node-uuid测试不太可能有用.我将在devoluk.com链接上更详细地评论.]

[Update: Just saw Veselin's report about the bug with Math.random() at startup. Since the problem is only at startup, the node-uuid test is unlikely to be useful. I'll comment in more detail on the devoluk.com link.]

这篇关于在JavaScript中生成UUID时发生冲突的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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