I'm trying to create globally-unique identifiers in JavaScript. I'm not sure what routines are available on all browsers, how "random" and seeded the built-in random number generator is, etc..
The GUID / UUID should be at least 32 characters and should stay in the ASCII range to avoid trouble when passing them around.
Here's a solution dated Oct. 9, 2011 from a comment by user jed at https://gist.github.com/982883:
This accomplishes the same goal as the current highest-rated answer, but in 50+ fewer bytes by exploiting coercion, recursion, and exponential notation. For those curious how it works, here's the annotated form of an older version of the function:
Here is a totally non-compliant but very performant implementation to generate an ASCII-safe GUID-like unique identifier.
Generates 26 [a-z0-9] characters, yielding a UID that is both shorter and more unique than RFC compliant GUIDs. Dashes can be trivially added if human-readability matters.
Here are usage examples and timings for this function and several of this question's other answers. The timing was performed under Chrome m25, 10 million iterations each.
Here is the timing code.
This create version 4 UUID (created from pseudo random numbers) :
Here is a sample of the UUIDs generated :
Well, this has a bunch of answers already, but unfortunately there's not a "true" random in the bunch. The version below is an adaptation of broofa's answer, but updated to include a "true" random function that uses crypto libraries where available, and the Alea() function as a fallback.
Here is a combination of the top voted answer, with a workaround for Chrome's collisions:
On jsbin if you want to test it.