在JavaScript中生成UUID时发生冲突

编程入门 行业动态 更新时间:2024-10-28 18:27:41
本文介绍了在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链接上更详细地评论.]

[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 link.]

更多推荐

在JavaScript中生成UUID时发生冲突

本文发布于:2023-10-22 09:26:14,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1517116.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:发生冲突   JavaScript   UUID

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!