Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
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"
]
}
@AdamRGrey
Copy link

AdamRGrey commented Nov 24, 2019

@mindplay-dk Can I use that?

@kizerkizer
Copy link

kizerkizer commented Nov 24, 2019

Who's reading this at 2018-11-02T09:07:56Z ?

[2019 CReW RePoRTiNG iN] ~~ like if you're still watching this epic gist in 2019 ~~ [2019 CReW RePoRTiNG iN]

Also, any resolution to the latest collision issues? Are people using this in production?

Beautiful snippet, btw.

@mindplay-dk
Copy link

mindplay-dk commented Nov 24, 2019

@AdamRGrey of course 😊

@RobertoMachorro
Copy link

RobertoMachorro commented Nov 24, 2019

Who's reading this at 2018-11-02T09:07:56Z ?

[2019 CReW RePoRTiNG iN] ~~ like if you're still watching this epic gist in 2019 ~~ [2019 CReW RePoRTiNG iN]

Also, any resolution to the latest collision issues? Are people using this in production?

Beautiful snippet, btw.

Using this in production, several projects, no collisions yet. I've considered changing that Math.random to something Date.now() based if it does happen, but if it ain't broke...

@AndrWeisR
Copy link

AndrWeisR commented Dec 5, 2019

This gist is a good example of the "Look at me, I'm a genius programmer who is so smart I can write unmaintainable code" disease.

@mindplay-dk
Copy link

mindplay-dk commented Dec 6, 2019

@AndrWeisR code golfing is harmless fun and a useful exercise that teaches you the details of the language. Would I put this in a project when I'm trying to collaborate with others? of course not, but golfing isn't just "genius programmers showing off", it's also just programmers challenging each other to practice, improve and have 𝒇𝒖𝒏™. 🙃

@Aaronius
Copy link

Aaronius commented Dec 6, 2019

A UUID generator is frequently treated as a black box by those who use one anyway. For such people, they'd rather have a black box with less code rather than a black box with more code, even if the code was the product of a nerdy online challenge. 🤷‍♂️

@sapher
Copy link

sapher commented Dec 14, 2019

This gist is a good example of the "Look at me, I'm a genius programmer who is so smart I can write unmaintainable code" disease.

Look at me "I'm jealous" disease. Take it as a challenge. It's just a gist, chill.

@PatrickDuncan
Copy link

PatrickDuncan commented Dec 30, 2019

Thank you for this. This is what I had to do for: TypeScript + ESLint

const getUuid = (a: string = ''): string => (
  a
    /* eslint-disable no-bitwise */
    ? ((Number(a) ^ Math.random() * 16) >> Number(a) / 4).toString(16)
    : (`${1e7}-${1e3}-${4e3}-${8e3}-${1e11}`).replace(/[018]/g, getUuid)
);
getUuid();

@webdeb
Copy link

webdeb commented Jan 8, 2020

finally I found a place to learn some code 👍

@luminarious
Copy link

luminarious commented Jan 18, 2020

Here's UUID v1. Doesn't fit in tweet 😒

// http://www.rfcreader.com/#rfc4122_line385 allows random instead of MAC address
// https://www.famkruithof.net/uuid/uuidgen
// https://realityripple.com/Tools/UnUUID/

// TODO: Refactor into generator function, so random clock ID stays uniform for the lifetime of application instance?
function uuid(c = 9999) {
  const t = ((Date.now() + 12219292800000) * 1E4).toString(16)
  const n = crypto.getRandomValues(new Uint8Array(6)).reduce((sum, x, i) => {
    return sum + ((i === 0) ? x|1 : x).toString(16).padStart(2, '0')
  }, '')

  return `${t.slice(-8)}-${t.slice(-12, -8)}-1${t.slice(0, 3)}-${c}-${n}`
}

// Give me some examples
for (let i = 100; i--; console.log(uuid())) {}

@jsejcksn
Copy link

jsejcksn commented Mar 22, 2020

Here's an ES-updated take on @mindplay-dk's non-golf version:

const uuidv4 = () => {
  const hex = [...Array(256).keys()]
    .map(index => (index).toString(16).padStart(2, '0'));

  const r = crypto.getRandomValues(new Uint8Array(16));

  r[6] = (r[6] & 0x0f) | 0x40;
  r[8] = (r[8] & 0x3f) | 0x80;
  
  return [...r.entries()]
    .map(([index, int]) => [4, 6, 8, 10].includes(index) ? `-${hex[int]}` : hex[int])
    .join('');
};

Has anyone written a version which doesn't use bitwise operators?

@mindplay-dk
Copy link

mindplay-dk commented Mar 24, 2020

@jsejcksn you probably should hoist your hex table to the parent scope, to avoid recalculating every time. Also note (though you might not care, depending on your use-case) that I was deliberately avoiding loops in my version for performance reasons - using map makes for fewer lines, but much slower execution. 🙂

@jsejcksn
Copy link

jsejcksn commented Mar 24, 2020

@mindplay-dk Thanks, it’s primarily for readability.

For max optimization, one of the other golfed versions could be used, or you could compile your code using an optimizing transpiler.

@dbones0527
Copy link

dbones0527 commented Apr 18, 2020

We can use coercion and exponential notation to get template string and save 7 bytes:
function(){return(""+1e7+-1e3+-4e3+-8e3+-1e11).replace(/1|0/g,function(){return(0|Math.random()*16).toString(16)})}

@ ### #

@haeric
Copy link

haeric commented Aug 1, 2020

We have been using this in production on the web, and are seeing a lot of collisions with the versions using Math.random in certain browsers. If you are using any of these snippets on the web, definitely use one that uses crypto.getRandomValues! Even if you tested it on your system and did not see any collisions, some other system/browser combination may implement Math.random in a way that causes easy collisions.

@chris-kruining
Copy link

chris-kruining commented Oct 8, 2020

It has been a huge treat to read this thread, so much positivity, creativity, knowledge and collaboration!

@jbvazquez
Copy link

jbvazquez commented Nov 3, 2020

Hi, what is the latest version of this function?

@phyreman
Copy link

phyreman commented Feb 25, 2021

The latest version I've got is this:

var uuid = () => ([1e7]+-1e3+-4e3+-8e3+-1e11).replace(/[018]/g,c =>(c^(window.crypto||window.msCrypto).getRandomValues(new Uint8Array(1))[0]&15>>c/4).toString(16));

@RobertoMachorro
Copy link

RobertoMachorro commented May 12, 2021

Is the OP not updating the code anymore? @phyreman how does your function differ?

@phyreman
Copy link

phyreman commented May 12, 2021

@RobertoMachorro Just ensuring the random numbers come from a crypto source instead of Math.random(). It's definitely slower than molasses though, lol. I think the OP stopped once it hit the point of basically not being able to golf it much more and a spec-compliant version was posted.

@Oman395
Copy link

Oman395 commented Nov 3, 2021

Ok I don't even know how, but I typed in 'var konami' with github copilot and it gave me:

var konami = { // https://gist.github.com/jed/982883
    "up": false,
    "down": false,
    "left": false,
    "right": false,
    "b": false,
    "a": false,
    "start": false,
}

how

@getify
Copy link

getify commented Feb 9, 2022

Can anyone explain the reasoning behind what seems rather arbitrary, in the replacing only 0s, 1s, and 8s with random hex digits, and not, for example, replacing all numbers with such digits? I'm sure I missed some detail that explains it.

@jed
Copy link
Author

jed commented Feb 9, 2022

IIRC the spec requires those digits to be certain values.

@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! :)

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