You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
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.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
[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 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 𝒇𝒖𝒏™. 🙃
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. 🤷♂️
// https://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?functionuuid(c=9999){constt=((Date.now()+12219292800000)*1E4).toString(16)constn=crypto.getRandomValues(newUint8Array(6)).reduce((sum,x,i)=>{returnsum+((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 examplesfor(leti=100;i--;console.log(uuid())){}
@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. 🙂
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)})}
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.
@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.
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 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. ;-)
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.
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 traceditback 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.
@AdamRGrey of course 😊