Collisions when generating UUIDs in 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:
So the questions are:
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.]
Indeed there are collisions but only under Google Chrome. Check out my experience on the topic here
http://devoluk.com/google-chrome-math-random-issue.html
Seems like collisions only happen on the first few calls of Math.random. Cause if you just run the createGUID / testGUIDs method above (which obviously was the first thing I tried) it just works with no collisions whatsoever.
So to make a full test one needs to restart Google Chrome, generate 32 byte, restart Chrome, generate, restart, generate...
Just so that other folks can be aware of this - I was running into a surprisingly large number of apparent collisions using the UUID generation technique mentioned here. These collisions continued even after I switched to seedrandom for my random number generator. That had me tearing my hair out, as you can imagine.
I eventually figured out that the problem was (almost?) exclusively associated with Google's web crawler bots. As soon as I started ignoring requests with "googlebot" in the user-agent field, the collisions disappeared. I'm guessing that they must cache the results of JS scripts in some semi-intelligent way, with the end result that their spidering browser can't be counted on to behave the way that normal browsers do.
Just an FYI.
链接地址: http://www.djcxy.com/p/2660.html上一篇: GUID和UUID之间是否有区别?