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:
What is causing this issue and how can I avoid it?
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
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.
[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.]