15
2
I need a UUID. Your job is to generate one.
The canonical UUID (Universally Unique IDentifier) is a 32 digit hexadecimal number with hyphens inserted in certain points.The program should output 32 hex digits (128 bits), in the form of xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx
(8-4-4-4-12
digits), where x
is a random hexadecimal number. Assuming that your language's PRNG is perfect, all valid outputs must have the same probability of being generated.
TL;DR
Generate 32 random hexadecimal digits in the form 8-4-4-4-12
digits. Shortest code wins.
EDIT: Must be hexadecimal. Always generating decimal only is invalid. EDIT 2: No built-ins. These aren't GUIDs, just generic hex digits.
Example output:
ab13901d-5e93-1c7d-49c7-f1d67ef09198
7f7314ca-3504-3860-236b-cface7891277
dbf88932-70c7-9ae7-b9a4-f3df1740fc9c
c3f5e449-6d8c-afe3-acc9-47ef50e7e7ae
e9a77b51-6e20-79bd-3ee9-1566a95d9ef7
7b10e43c-3c57-48ed-a72a-f2b838d8374b
Input, and standard loopholes are disallowed.
This is code-golf, so shortest code wins. Also, feel free to ask for clarifications.
1To be clear, we should print one randomly generated UUID, yes? – Dennis – 2015-09-22T04:08:59.940
5
Seems like a less strict version of http://codegolf.stackexchange.com/q/32309/14215
– Geobits – 2015-09-22T04:09:45.220@Dennis: correct. – clap – 2015-09-22T04:12:57.430
@Geobits: true, but not a duplicate :P – clap – 2015-09-22T04:13:18.717
9"These examples are not random. Try to attach some significance." What does that mean? – Alex A. – 2015-09-22T04:15:22.317
@Alex it means exactly that. They're not random, I took them from somewhere. – clap – 2015-09-22T04:16:14.923
I don't think this is a duplicate. Sure, the intended output is strikingly similar, but all answers from Generate four type-4 GUIDs for me only use 448 bits of entropy.
– Dennis – 2015-09-22T04:16:26.887@AlexA. I took the idea from here
– clap – 2015-09-22T04:17:50.3173Actually, one does not need hexadecimal numbers, 10-base can also be random. For example,
12345678-1234-1234-1234-123456789012
should be a valid UUID (or is any hex digit necessary?). Do you consider this a loophole? – Voitcus – 2015-09-22T07:26:25.7603The title and first sentence suggest that you want a canonical UUID, and the examples given appear to follow the spec for UUIDs, but you actually seem to be asking for something else. – Peter Taylor – 2015-09-22T08:48:51.083
2The required format user version 1 to version 5 and invalid formats for a UUID. Please, read about version 4 and decide what you want. – Ismael Miguel – 2015-09-22T09:42:13.367
3I feel compelled to point out that the version 4 (random) UUID has a required format of
xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx
wherey
is one of[89AB]
. At the time of this comment, none of the answers (except C# using a built in library) are guaranteed to produce a valid random UUID (and actually, are quite likely to not produce one). – None – 2015-09-22T13:59:36.990This challenge isn't specifically for any version of UUIDs, but just generating a generic
8-4-4-4-12
string. The output strings look to be UUIDv4 but there's already a UUIDv4 challenge. – clap – 2015-09-22T14:11:29.120@Voitcus It could be a loophole, but I specified in the question that it should be in hexadecimal. – clap – 2015-09-22T14:12:20.577
@ConfusedMr_C I can add a letter
A
at the end of the output string and there will be hexadecimal digit. In my opinion your question is not clear. – Voitcus – 2015-09-23T07:45:59.857I'd suggest the following wording: Assuming that your language's PRNG is perfect, all valid outputs must have the same probability of being generated. – Dennis – 2015-09-24T19:59:09.623
Ooh, thanks! :D I'll use that. – clap – 2015-09-25T02:42:32.347