/    Sign up×
Community /Pin to ProfileBookmark

Guid randomness.

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 + “}”;
}[/CODE]

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?

to post a comment
JavaScript

3 Comments(s)

Copy linkTweet thisAlerts:
@fischermxauthorJun 01.2006 — Any idea ?
Copy linkTweet thisAlerts:
@mrhooJun 01.2006 — I know that the javascript random number generator is the way to get a

pseudo random number, but it's a good idea to test the result and get another one

if the id is already in use. Actually, 6 in 12000 sounds about right for the

range you're working in.

It's not Really Random- it is calculated, and it is pretty good,

but not as good as the ones the lottery uses.
Copy linkTweet thisAlerts:
@fischermxauthorJun 01.2006 — I can't test for duplicate to regenerate since I'm creating this remotely and my code would not allow to return in case of duplicate. Neither checking previously for duplicate is a good idea since it would double my calls and traffic.

I thought it could be with the seed. So I modified my code to seed the random generator. Still I have 4 duplicates today.

I can't believe how a person in Vienna, in WebSite X, download my js and generate a 32 digit random number and then it coincides with someone in NewYork, visiting website Y.

I think 32 digits would be long enough.
×

Success!

Help @fischermx spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 6.18,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @nearjob,
tipped: article
amount: 1000 SATS,

tipper: @meenaratha,
tipped: article
amount: 1000 SATS,

tipper: @meenaratha,
tipped: article
amount: 1000 SATS,
)...