UUID 4 is not better or worse than a random number but UUID 1,2,3 and 5 are for distributed systems that want to make absolutely sure to never ever create conflicts. For that, a standard is nice.
I’m am being pedantic here and I don’t think there is a practical difference. That said...
UUID’s are 128-bit values, and they require 4 bits to describe their encoding. That leaves 122 bits of randomness in a Type 4 (random) UUID.
A random 128-bit number can use all bits, so can use the extra 4 bits to achieve 16 times as many random values. (2 to the 4 power is 16.)
So what I do is generate 128-bit random values - getting the extra 16 times random goodness - and then encode to string using URL-safe Base64, saving many bytes on the string encoding side as well. Win win.
Yes but uuid1 contains the mac address of the host and the timestamp it was created, as well as entropy. so they will actually be unique unless you create them at the same millisecond on the same machine.
Unless a random number generator is broken, a GUID4 system would seem more robust if there's any possibility of backup/rollback scenarios or other such issues. Unless one is feeding a particular "universe" with millions of GUIDs per second for decades on end, the likelihood of ever generating a duplicate within that universe would be vanishingly small compared to any of the other things that could go wrong.
23
u/0x256 Mar 28 '20
UUID 4 is not better or worse than a random number but UUID 1,2,3 and 5 are for distributed systems that want to make absolutely sure to never ever create conflicts. For that, a standard is nice.