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.
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 usescrypto.getRandomValues
! Even if you tested it on your system and did not see any collisions, some other system/browser combination may implementMath.random
in a way that causes easy collisions.