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

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

问题描述

这与此问题有关。我正在使用此答案生成UUID在JavaScript中:

This relates to this question. I am using this answer to generate 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, however i am getting collisions. Here's what i have:


  • 在谷歌浏览器中运行的网络应用程序。

  • 16位用户。

  • 这些用户在过去2个月内已经生成了大约4000个UUID。

  • 我发生了大约20次碰撞 - 例如今天生成的新UUID与大约2个月前相同(不同的用户)。

  • A web-app running in Google Chrome.
  • 16 users.
  • about 4000 UUIDs have been generated in the past 2 months by these users.
  • i got about 20 collisions - e.g. new UUID genereated today was the same as about 2 months ago (different user).

所以问题是:


  1. 造成这个问题的原因是什么?

  2. 我该如何避免它?


推荐答案

我最好的猜测是 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关于数学错误的报告。启动时随机()。由于问题仅在启动时, 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天全站免登陆