Skip to content

Instantly share code, notes, and snippets.

@telendt
Created July 4, 2012 22:19
Show Gist options
  • Save telendt/3049799 to your computer and use it in GitHub Desktop.
Save telendt/3049799 to your computer and use it in GitHub Desktop.
uuid4.js
function uuid4() {
var r = Math.random,
p = "0000000",
n = 0x100000000,
a = (r() * n >>> 0).toString(16),
b = ((r() * n & 0xffff4fff | 0x4000) >>> 0).toString(16),
c = ((r() * n & 0xbfffffff | 0x80000000 ) >>> 0).toString(16),
d = (r() * n >>> 0).toString(16);
if (a.length < 8) {
a = (p + a).slice(-8);
}
if (b.length < 8) {
b = (p + b).slice(-8);
}
if (c.length < 8) {
c = (p + c).slice(-8);
}
if (d.length < 8) {
d = (p + d).slice(-8);
}
return [
a,
b.slice(0,4),
b.slice(4),
c.slice(0,4),
c.slice(4) + d
].join("-");
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment