9 ms·
16 random bytes is not a valid UUIDv4. I don’t think it needs to be in the standard library, but implementing the spec yourself is also not the right choice for
by computomatic 6mo ago
16 random bytes is not a valid UUIDv4. I don’t think it needs to be in the standard library, but implementing the spec yourself is also not the right choice for 99% of cases.
- rollulus 6mo agoWell that depends on your luck, it could be a valid one about 1/16th of the time.
- jasomill 6mo ago1/64, actually, because RFC-compliant (variant 1) UUIDv4 requires fixed values for both the version nibble and two bits of the variant nibble. The fact that we're discussing this at all is a reasonable argument for using a library function.
- rollulus 6mo agoNice, thanks and I agree.
- 3eb7988a1663 6mo agoWhile it might be invalid, will most libraries choke if you give them a pseudo UUIDv4?
- efilife 6mo agoWhat do you mean? Are you talking about validation of UUIDs?
- 3eb7988a1663 6mo agoIf you generate random bytes, which are unlikely to conform to the UUIDv4 spec, my guess is that most libraries will silently accept the id. That is, generating random bytes, will probably work just work.
- efilife 6mo agoBut what libraries are you talking about? What is their purpose?
- koakuma-chan 6mo agoWhat if I generate 16 random bytes and use that as id?
- usrnm 6mo agoNo problem, just don't call it UUID
- groestl 6mo agoLet's call it a valid UUIDv0 - all bits randomized including the version bits :)
- vbezhenar 6mo agoI didn't say about 16 random bytes. But you're almost there. You generate 16 random bytes and perform few bitwise operations to set version and variant bits correctly. Not that it matters. I don't even think that there's a single piece of software in the world which would actually care about these bits rather than treating the whole byte array as opaque thing.