Tuesday, January 16, 2007

Creating GUIDs with Client Side JavaScript

I needed to generate GUIDs (globally unique identifiers) for a web project I was working on.
The only solutions I could see for that task involved evoking an ActiveX control which makes it really useless for general purpose. I ended up writing a short javascript that will achieve this task:

function generateGuid()
{
var result, i, j;
result = '';
for(j=0; j<32; j++)
{
if( j == 8 || j == 12|| j == 16|| j == 20)
result = result + '-';
i = Math.floor(Math.random()*16).toString(16).toUpperCase();
result = result + i;
}
return result
}

15 comments:

Ray of Light said...

Thank you for this code. It really came in handy for me in a project of mine.

Sagi E. Shkedy said...

I am glad I could help :)

Ryan Shillington said...

I'm not trying to be rude - just to help. This isn't a GUID generator. a GUID is a "Globally Unique Identifier", which means that if everybody in the world created one, then no two of the GUIDs would ever be the same.

Since this GUID is based only on a random number (and doesn't include say an IP address), two people could easily get the same GUID. Since Javascript's random number generator is based on the current time, all it would take is for two users to create a GUID at the same time of day (to the millisecond), which in any reasonably well used application isn't inconceivable.

Sagi E. Shkedy said...

Ryan,
You make a vaild point. In general it is very hard to generate "real" random number due to the need for a seed that itself be - random. Now for this algorithm we use 32 random selections of digits to generate the number. With that said, even if those two machines you mentioned, both starting at the same time get the same digit they will have to be in complete sync in order to generate identical numbers which is very unlikely.
There is always a chnnce of repitition but I belive that it is very low.

M@ said...

I've got a need for a guid on my clients, and I think I'm going to have to break down and make the server generate the guid. The reason lies in the difference between "Random" and "Unique." Generating random number will give you a low probability of having two clients use the same number - but it's not a zero probability. A real "guid" generated with a technique the ensures that number will never, ever be generated again is something that I think can only be done on a server which uses something like it's IP address + NIC card identifier and only generates them one at a time and uses the system time as part of the formula.

Unknown said...

I have a similar problem...we need to create a *unique* GUID - we cannot even accept a low probability of two PCs having identical values. This is in part b/c if at some point they need to be regenerated, the exact same GUID must be generated for the same PC as the last time. And we need to do this client-side using either JS or Flash (unless someone has a better idea). Can anyone make any suggestions? It also needs to work cross-OS and cross-platform, so ActiveX is not an option. Thanks!

Unknown said...

I have a similar problem...we need to create a *unique* GUID - we cannot even accept a low probability of two PCs having identical values. This is in part b/c if at some point they need to be regenerated, the exact same GUID must be generated for the same PC as the last time. And we need to do this client-side using either JS or Flash (unless someone has a better idea). Can anyone make any suggestions? It also needs to work cross-OS and cross-platform, so ActiveX is not an option. Thanks!

Unknown said...
This comment has been removed by the author.
Sagi E. Shkedy said...

lgodiva,
It sounds like what you need is not exactly a GUID, you need some kind a signiture that will globally identify a browser and then making sure that you will be able to recreate the same number again given that you encounter the same client.
The idea with GUID generator is that you create a differne number every time you rin it. I think what you are looking for is some kind of a hash function that will give you a low probablility of collisions given that you met the same machine. This is not trivial as IPs change, you do not have the MAC address of the computer or anything that is globally identifiable, I am guessing things are done that way to protect privacy.
The way I would resolve this is to generate a GUID(on the sever or on the client).
When the client makes a request the GUID as cookie on the client. On future requests you will access the GUID by reading the cookie.

Billy said...

I would just like to point out that collisions have a 16^32 chance of happening.

That's 1 in 340,282,367,000,000,000,000,000,000,000,000,000,000

or about half the atoms in the observable universe.

And ignoring that, guids are never 100% -- there can always be a collisions, just with md5 hashes.

Thanks for the code -- I will use it :)

Wenbert Del Rosario said...

thank you for this.
i hope it is okay with you if i use this code. i have your link commented with the code ;)

Sagi E. Shkedy said...

Go right ahead.

Ruchir said...

Thank you Sagi, your code helped me big time!

5 little speckled frogs said...

Good stuff. Thanks for posting this function. I need it to help create unique id's for cloaned object using drag-drop in jquery.

Gibatronic said...

this is a nice solution, but you don't need that if, just change your for:
for (j = 8; j <= 20; j += 4)

namastĂȘ!