I once tested the GUID generated by SQL Server by creating a database with a GUID primary key and ran a test. I filled the database during several hours reaching millions of records without a PK violation, i.e. it generated almost perfect unique ids, or for that case, perfect.
But now I need to generate a sort of unique ID from javascript, to use in a crazy project I’m doing, and I found this code here :
[CODE]
function newGuid()
{
var g = “{“;
for(var i = 0; i < 32; i++)
g += Math.floor(Math.random() * 0xF).toString(0xF) + (i == 8 || i == 12 || i == 16 || i == 20 ? “-” : “”)
return g + “}”;
}
I’m using this to generate an ID on the client, the send back to the server which in turn, creates a cookie and saves it in a database (it is not a primary key, but I need unique ids anyway).
So, I’m filling this to a database that backup a website. For my surprise in a 12,000 records table I already have 6 duplicates !!
Why ? I can’t believe it, is like winning the lotery! I’m creating thirty-two digits with 1 to 16 values and I get a duplicated, actually 6 pairs of duplicated records in such small sample ?
Do you think is that possible ?
Could be another crazy thing that I am missing ?
Does anybody knows how does this math.random function works internally?