Collisions when generating UUIDs in JavaScript?

前端 未结 6 1862
遥遥无期
遥遥无期 2020-11-28 20:12

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

\'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx\'.replace(/[         


        
6条回答
  •  臣服心动
    2020-11-28 20:58

    The answers here deal with "what's causing the issue?" (Chrome Math.random seed issue) but not "how can I avoid it?".

    If you are still looking for how to avoid this issue, I wrote this answer a while back as a modified take on Broofa's function to get around this exact problem. It works by offsetting the first 13 hex numbers by a hex portion of the timestamp, meaning that even if Math.random is on the same seed it will still generate a different UUID unless generated at the exact same millisecond.

提交回复
热议问题