# 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 , so shortest code wins. Also, feel free to ask for clarifications.

• Seems like a less strict version of codegolf.stackexchange.com/q/32309/14215 Sep 22 '15 at 4:09
• "These examples are not random. Try to attach some significance." What does that mean? Sep 22 '15 at 4:15
• Actually, 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? Sep 22 '15 at 7:26
• The 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. Sep 22 '15 at 8:48
• I feel compelled to point out that the version 4 (random) UUID has a required format of xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx where y 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).
– user12166
Sep 22 '15 at 13:59

# Chip, 109 + 6 = 115 bytes

Requires flags -wc36, causing +6 bytes

!ZZZZZZZZZZZZZZZZZZZZZZ
,-----.,+vv--^----^---z
?]]]--R\acd
?xx+-)\\b
?x+x-)\\c
?^xx\--\d
-xx]v~\e
f*)'-\g


Try it online!

Generates 4 random bits (the four ?'s) and converts to hex digits:

• 0x0 - 0x9 => 0 - 9
• 0xa - 0xe => b - f
• 0xf => a

...a bit unconventional, but it saved me some bytes at no expense to the distribution of outcomes.

# Ruby, 51 47 + 14 = 65 61 bytes

Run with ruby -rsecurerandom (+14 bytes)

[8,4,4,4,12].map{|n|SecureRandom.hex n}.join ?-


Returns output

# Java, 192 181 bytes

interface T{static int t(){return (int)(Math.random()*65536);}static void main(String[]a){System.out.printf("aa-a-a-a-aaa".replaceAll("a","%04x"),t(),t(),t(),t(),t(),t(),t(),t());}}


## Octave, 10610510399 96 bytes

function[]=f,g=@(l)[48:57,'a':'f'](randi(16,[1 l]));strjoin({g(8),g(4),g(4),g(4),g(12)},'-'),end


Try it Online

# Pyth, 34 bytes

VS32 aYO16)s.e?}km*d4r2 6+\-bb.HMY


Try it online!

trying to generate the indexes of where the "-" should go, and doing the replacement in a smaller way than hardcoding was an interesting challenge, and probably the hardest part.

### Explanation

VS32 aYO16)                          // for N in range(32), append a random number 0-15 to the list Y
.HMY   // convert each number in Y to its Hex equivalent
m*d4r2 6            // make an array (of indexes) containing [8 12 16 20]. *4 mapped over (range(2,6) -> [2,3,4,5])
.e                       // enumerating map on the random hex list. For each digit, index is k and value is b.
?}k        +\-bb       // map onto each digit the ternary question "is index k in the array of idexes?"
// if no, replace with b (the value that was already there), if yes replace it with "-b".
s                         // "".join( ) to make the list into a string


1 FOR I=1 TO 32:PRINT HEX$(INT(RND*16));:IF I=8 OR I=12 OR I=16 OR I=20 THEN PRINT"-"; 2 NEXT  Generates 32 "random" hex digits, adding a hyphen in the appropriate places. Output: D36A49D6-85AC-51DC-6D84-3BB1EB1A0816  # Tcl, 95 bytes time {append g [format %x[expr [incr i]%4|$i<7|$i>21?"":"-"] [expr int(rand()*16)]]} 32;puts$g


Try it online!

time {append g [format %x[expr [incr i]%4||$i<7||$i>21?"":"-"] [expr int(rand()*16)]]} 32;puts $g  Try it online! # Tcl, 98 bytes time {incr i;append g [format %x[expr$i%4||$i<7||$i>21?"":"-"] [expr int(rand()*16)]]} 32;puts $g  Try it online! # Tcl, 101 bytes time {incr i;append g [format %x[expr !($i%4)&&$i>7&&$i<21?"-":""] [expr int(rand()*16)]]} 32;puts $g  Try it online! # Tcl, 104 bytes proc D {n\ 4} {time {append g [format %x [expr int(rand()*16)]]}$n;set g}
puts [D 8]-[D]-[D]-[D]-[D 12]


Try it online!

# Pushy, 34 bytes

12N4448s$:Z15U&T)?39+;48+'.;L?45'.  Try it online! 12 \ Push 12 to stack N \ Remove printing delimiter (normally a newline) 4448s \ Push 4448, then split into digits \ The stack is now [12, 4, 4, 4, 8]$                          \ While items left on stack:
:                         \ (top item) times do:
Z15U                     \    Push random(0, 15)
&T)?                 \    If bigger than 10 (a hex digit):
39+;             \      Add 39 (offset from ASCII numbers to lowercase)
48+          \   Add 48, converting it into the correct character
'.        \   Print and pop this character
;L?45'. \ Print a dash ('-'), unless stack is now empty.



# 05AB1E, 19 bytes

ŽΣÌvy4*F15ÝΩh}'-}J¨


Try it online.

Explanation:

ŽΣÌ                   # Compressed integer 21113
v            }     # Loop over each of its digits y
y4*               #  Multiply y by 4
F     }        #  Loop that many times
15Ý           #   Create a list in the range [0,15]
Ω          #   Pick a random number from it
h         #   Convert it to hexadecimal
'-     '#  Push string "-"
J    # After the loops, join the entire stack together
¨   # And remove the trailing "-" (and output the result implicitly)


See this 05AB1E tip of mine (section How to compress large integers?) to understand why ŽΣÌ is 21113.

# JavaScript (ES6), 79 bytes

a=>[8,4,4,4,12].map(g=e=>e?(16*Math.random()|0).toString(16)+g(e-1):"").join-

• The function isn't running for me. Apr 8 '20 at 7:19

## Javascript (ES6) - 102 bytes

### UUID v4 compliant

A compaction of broofa's solution on Stackoverflow:

"10000000-1000-4000-8000-100000000000".replace(/[018]/g,s=>(s^Math.random()*256&15>>s/4).toString(16))


# Python 3, 103 bytes

from random import*;print(''.join(hex(randint(2**(i*3),2**(3*i+2)))[2:]+'-'for i in [8,4,4,4,12])[:33])
`

Try it online!

Is 100% improvable...