jeudi 20 août 2015

Is this *really* more random?

Consider this question:

Create GUID / UUID in JavaScript?

My question is:

Is Briguy37's answer (using new Date().getTime();):

var d = new Date().getTime();
var uuid = 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'.replace(/[xy]/g, function(c) {
    var r = (d + Math.random()*16)%16 | 0;
    d = Math.floor(d/16);
    return (c=='x' ? r : (r&0x3|0x8)).toString(16);
});

really more random than for instance broofa's?

'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);
});

I would imagine that Math.random tries to use a unique seed anyway.. probably generated in part from the time. This is why I wonder if this adds anything, or not.




Aucun commentaire:

Enregistrer un commentaire