Skip to content

Instantly share code, notes, and snippets.

@jed
Forked from 140bytes/LICENSE.txt
Created May 20, 2011 13:27
Show Gist options
  • Save jed/982883 to your computer and use it in GitHub Desktop.
Save jed/982883 to your computer and use it in GitHub Desktop.
generate random UUIDs

UUID

Returns a random v4 UUID of the form xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx, where each x is replaced with a random hexadecimal digit from 0 to f, and y is replaced with a random hexadecimal digit from 8 to b.

There's also @LeverOne's approach using iteration, which is one byte shorter.

function b(
a // placeholder
){
return a // if the placeholder was passed, return
? ( // a random number from 0 to 15
a ^ // unless b is 8,
Math.random() // in which case
* 16 // a random number from
>> a/4 // 8 to 11
).toString(16) // in hexadecimal
: ( // or otherwise a concatenated string:
[1e7] + // 10000000 +
-1e3 + // -1000 +
-4e3 + // -4000 +
-8e3 + // -80000000 +
-1e11 // -100000000000,
).replace( // replacing
/[018]/g, // zeroes, ones, and eights with
b // random hex digits
)
}
function b(a){return a?(a^Math.random()*16>>a/4).toString(16):([1e7]+-1e3+-4e3+-8e3+-1e11).replace(/[018]/g,b)}
DO WHAT THE FUCK YOU WANT TO PUBLIC LICENSE
Version 2, December 2004
Copyright (C) 2011 Jed Schmidt <http://jed.is>
Everyone is permitted to copy and distribute verbatim or modified
copies of this license document, and changing it is allowed as long
as the name is changed.
DO WHAT THE FUCK YOU WANT TO PUBLIC LICENSE
TERMS AND CONDITIONS FOR COPYING, DISTRIBUTION AND MODIFICATION
0. You just DO WHAT THE FUCK YOU WANT TO.
{
"name": "UUID",
"description": "Generates random UUIDs",
"keywords": [
"UUID",
"ID",
"random"
]
}
@getify
Copy link

getify commented Feb 9, 2022

@jed thanks for the link. I browsed through that spec to see if I could find something that jumps out. I see lots of discussion of bits in fields and such, but nothing that obviously connects digits 0, 1, and 8 to needing to be replaced. In fact, the part you linked to specifically says:

Set all the other bits to randomly (or pseudo-randomly) chosen values.

In any case, appreciate having further info. My understanding is still incomplete, but it's less incomplete now. ;-)

@getify
Copy link

getify commented Feb 9, 2022

Oh, wait... I think I maybe get more of it now... the code has stuff like 1e3 and 8e3, which are like 1000 and 8000... but it also has 4e3... so basically it's targeting all the 1s, 0s, and 8s for replacement, but leaving the 4 in the middle alone.

@jed
Copy link
Author

jed commented Feb 9, 2022

That's because it's not part of the spec, it's part of the genius of @subzey. Look at the string that's being replaced

  • 0 and 1 (1 is used as the head of a segment to prevent truncation),
  • 8, which is special cased with the ^ operation to clamp it to the proper values, but not
  • 4, which cannot be replaced because it specifies the version in the spec.

@jed
Copy link
Author

jed commented Feb 9, 2022

right, you got it. the 1 could be any other digit, really, it's just a placeholder, but the 8 was selected for its bitwise operation properties.

@jed
Copy link
Author

jed commented Feb 9, 2022

(there's a walkthrough here, fwiw)

@getify
Copy link

getify commented Feb 9, 2022

@jed thanks! :)

@tylermercer
Copy link

I was perusing the minified source of a website to see how they did something, came across this interesting way to create a GUID, and traced it back to here. I thought it was interestingly minified in the source I was looking at (with an insanely clever minifier!). Made me laugh when I saw that the original was golfed. 😆 A+ on the golfing, BTW! Clever minifier indeed--a clever human minifier. This was really fun to mentally chew on.

@liath
Copy link

liath commented Oct 3, 2023

For funsies, here's a rough draft on a UUIDv7 generator:

[...Date.now().toString(16).padStart(12,0)].toSpliced(8,0,'-').join``+([-7e3]+-8e3+-1e11).replace(/[018]/g,b=>(b^crypto.rng(1)[0]%16>>b/4).toString(16))

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment