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

查看:25
本文介绍了在 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 与大约两个月前相同(不同的用户).

是什么导致了这个问题,我该如何避免?

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天全站免登陆