logoalt Hacker News

computomatictoday at 7:30 AM4 repliesview on HN

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.


Replies

vbezhenartoday at 11:24 AM

I 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.

rollulustoday at 7:40 AM

Well that depends on your luck, it could be a valid one about 1/16th of the time.

show 1 reply
groestltoday at 9:12 AM

Let's call it a valid UUIDv0 - all bits randomized including the version bits :)

koakuma-chantoday at 7:50 AM

What if I generate 16 random bytes and use that as id?

show 1 reply