Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
generate random v4 UUIDs (107 bytes)

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.

Instance, based on recursion, can be found here.

function(
a,b // placeholders
){
for( // loop :)
b=a=''; // b - result , a - numeric variable
a++<36; //
b+=a*51&52 // if "a" is not 9 or 14 or 19 or 24
? // return a random number or 4
(
a^15 // if "a" is not 15
? // genetate a random number from 0 to 15
8^Math.random()*
(a^20?16:4) // unless "a" is 20, in which case a random number from 8 to 11
:
4 // otherwise 4
).toString(16)
:
'-' // in other cases (if "a" is 9,14,19,24) insert "-"
);
return b
}
function(a,b){for(b=a='';a++<36;b+=a*51&52?(a^15?8^Math.random()*(a^20?16:4):4).toString(16):'-');return b}
DO WTF YOU WANT TO PUBLIC LICENSE
Version 2, December 2004
Copyright (C) 2011 Alexey Silin <pinkoblomingo@gmail.com>
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 WTF YOU WANT TO PUBLIC LICENSE
TERMS AND CONDITIONS FOR COPYING, DISTRIBUTION AND MODIFICATION
0. You just DO WTF YOU WANT TO.
{
"name": "UUID",
"description": "Generates random UUIDs",
"contributors": [
"jed",
"subzey",
"tsaniel"
],
"keywords": [
"UUID",
"ID",
"random"
]
}
@jed

This comment has been minimized.

Copy link

jed commented Oct 24, 2011

nice work, @LeverOne. it's 1 byte shorter and faster too: http://jsperf.com/uuid-golf

@subzey

This comment has been minimized.

Copy link

subzey commented Oct 24, 2011

Well done! And very nice ~a%5 trick.

@tsaniel

This comment has been minimized.

Copy link

tsaniel commented Oct 24, 2011

I really want to know why ~a%5|a*3&4 matches 9 or 14 or 19 or 24.
Anyway, excellent @LeverOne!

@jed

This comment has been minimized.

Copy link

jed commented Oct 24, 2011

for those keeping track, this snippet manages to use 11 unique operators: =, ++, <, +=, ~, %, |, *, &, ?:, and ^.

@LeverOne

This comment has been minimized.

Copy link
Owner Author

LeverOne commented Oct 24, 2011

Thanks all! List of operations can be extended. We can use "a-15?...". But "^" faster.

@subzey

This comment has been minimized.

Copy link

subzey commented Oct 25, 2011

@LeverOne, - operator should be faster than ^ (and it is, for about 10% in node.js). ^ uses Double ↔ Int32 conversion twice thus making significant overhead.

UPD: I was wrong, ^ is faster indeed in IE9. There's no visible difference between ^ and - cases in other browsers.

@subzey

This comment has been minimized.

Copy link

subzey commented Oct 25, 2011

Cut one byte:

function(a,b){for(b=a='';a++<36;b+=4<<~a*6.5?(a^15?8^Math.random()*(a^20?16:4):4).toString(16):'-');return b}
@LeverOne

This comment has been minimized.

Copy link
Owner Author

LeverOne commented Oct 26, 2011

@subzey, fork this gist and put your code, please!
I don't want to speak the words that you know yourself. :))

@subzey

This comment has been minimized.

Copy link

subzey commented Oct 26, 2011

I suppose, this change is minor enough.

Unfortunately, gists can be forked but cannot be merged back. In order to avoid huge amount of hardly-ever traversable branches, let's imagine I've made a fork and then send you a pull request.

@LeverOne

This comment has been minimized.

Copy link
Owner Author

LeverOne commented Oct 26, 2011

// 107
function(a,b){for(b=a='';a++< 36;b+=a * 51&52?(a^15?8^Math.random()*(a^20?16:4):4).toString(16):'-');return b}

Everything is just beginning! // <-- No, this is the end.

@subzey

This comment has been minimized.

Copy link

subzey commented Oct 26, 2011

Great!

@jed

This comment has been minimized.

Copy link

jed commented Oct 27, 2011

just incredible. @LeverOne, did you try using the negative number coercion trick instead of using hyphens?

@tsaniel

This comment has been minimized.

Copy link

tsaniel commented Oct 27, 2011

@jed: It won't work when the number is 0.

@jed

This comment has been minimized.

Copy link

jed commented Oct 27, 2011

good point, @tsaniel.

@gmilby

This comment has been minimized.

Copy link

gmilby commented Aug 11, 2012

negative integers failed also.

@nijikokun

This comment has been minimized.

Copy link

nijikokun commented Mar 1, 2013

Collisions are high in this sector.

@develCuy

This comment has been minimized.

Copy link

develCuy commented Apr 11, 2013

Included into Ophal, congrats for the great work!
ophal/core@d7a4c66

@drzhbe

This comment has been minimized.

Copy link

drzhbe commented Jun 20, 2015

Wow.
@LeverOne how you found out this a*51&52? I mean you was staring at bit patterns of 9, 14, 19 and 24 and suddenly came out with this 52? Or how?

@c-hild

This comment has been minimized.

Copy link

c-hild commented Aug 18, 2016

USER TICKET #1
feature request

Even Out Character Distribution

As a user, not a developer, of this fine node module, I have to say after reading the installation instructions I have but one gripe, and it is a very specific (if minor) one.

For installing a new module (in this case typing it into my editor by reading it from my napkin that I took to my friend's or something) I do not at all mind using all of the keys on my 105 key keyboard, but I'd love to avoid using one key many times and another not at all. You know, in order to have the color on the caps wear off evenly. If I have to install this module a bunch of times I mean.

It's fine for numbers and symbols to be a bit more frequent than letters, which I use a lot for other languages, mostly natural ones. But can this be evened out a bit in general? The characters seem repetitive at first glance.

Thank you for the module though. Oh, and a mnemonic would be nice so I don't always need the napkin for installation. Might make that another ticket.

@stuartpb

This comment has been minimized.

Copy link

stuartpb commented Nov 28, 2016

Okay, I THINK I'm on track to understanding the trick behind the a*51&52 magic now (I suspect it functions similarly to this technique to determine if a number is a power of 2 - note that, due to the bodiless design of this for, where all evaluation is done in the post-loop "increment" statement, this procedure uses one-based indices and never has to deal with the zero case); I'm going to make a tool to see if I can refine the method for discovery.

@LiamKarlMitchell

This comment has been minimized.

Copy link

LiamKarlMitchell commented Dec 26, 2018

Shout out to arrow functions shaving off 5 characters.

(a,b)=>{for(b=a='';a++<36;b+=a*51&52?(a^15?8^Math.random()*(a^20?16:4):4).toString(16):'-');return b})

Awesome tricks btw.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.