Collisions when generating UUIDs in JavaScript?

前端 未结 6 1869
遥遥无期
遥遥无期 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 21:00

    I wanted to post this as a comment to your question, but apparently StackOverflow won't let me.

    I just ran a rudimentary test of 100,000 iterations in Chrome using the UUID algorithm you posted and got no collisions. Here's a code snippet:

    var createGUID = function() {
        return '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);
        });
    }
    
    var testGUIDs = function(upperlimit) {
        alert('Doing collision test on ' + upperlimit + ' GUID creations.');
        var i=0, guids=[];
        while (i++

    Are you sure there isn't something else going on here?

提交回复
热议问题