The type 4 GUID is described by Wikipedia, quoth:

Version 4 UUIDs use a scheme relying only on random numbers. This algorithm sets the version number (4 bits) as well as two reserved bits. All other bits (the remaining 122 bits) are set using a random or pseudorandom data source. Version 4 UUIDs have the form xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx where x is any hexadecimal digit and y is one of 8, 9, A, or B (e.g., f47ac10b-58cc-4372-a567-0e02b2c3d479).

Write a program that loads 488 bits (61 bytes) of cryptographic quality randomness and output them as 4 GUIDs using the format quoted above.

  • Do not waste randomness.
    • All random bits must appear in the encoded output.
    • All encoded bits must be traceable to either a single random bit or one of the six fixed bits that must appear in each GUID.
  • Use the cryptographic quality random number generator supplied by your language's standard library.
  • Do not use a library or external resource that generates GUIDs.
  • The hyphens must appear in their correct positions.
  • The fixed bits of this form of GUID must appear in their correct positions.
  • You may use a standard library function designed to output a GUID from 128 bits.
  • Normal code-golf rules apply.

For example...

> gentype4

I asked for four GUIDs in order to allow a whole number of random bytes to be gathered without leaving any remainder. I also specified that none of the bits should be wasted in order to avoid the easy answer of getting 16 random bytes and dropping the 4 in. (No, shift the other bits along first.)

Update 2:
If your language's standard library doesn't have a cryptographic quality random byte generator, you may instead assume that there is a 61 byte file present with enough random bytes for you to use when the program starts. The file should be called "/dev/urandom" or another filename with 12 characters.

  • \$\begingroup\$ If my language doesn't have a way to request a certain number of random bits/bytes, but can get a random number from 0 to n-1 using something like random(n), can I just use random(1<<k) as a way to get k random bits? Also, is it ok to get them in small pieces (e.g. 4 or 2 bits at a time) through multiple calls? \$\endgroup\$ Jun 23, 2014 at 16:48
  • \$\begingroup\$ I was hoping to see how different languages can access cryptographic quality randomness. I'll add an exception for those languages without such means. \$\endgroup\$
    – billpg
    Jun 23, 2014 at 16:52
  • \$\begingroup\$ Well, now you're assuming it can read external files :p What I could use is a sequence of numbers (separated by space) given through the standard input.. but maybe I'm asking too much and should just not participate (in that language). And an unrelated note: the wikipedia quote refers to UUIDs, not GUIDs. \$\endgroup\$ Jun 23, 2014 at 17:07
  • \$\begingroup\$ A close duplicate: codegolf.stackexchange.com/q/20363 \$\endgroup\$
    – primo
    Jun 24, 2014 at 11:37

10 Answers 10


PHP, 338 bytes

<?php $r=openssl_random_pseudo_bytes(61);$b="";$s="substr";for($i=0;$i<61;)$b.=str_pad(decbin(ord($r[$i++])),8,0,0);function p($B,$L){return str_pad(dechex(bindec($B)),$L,0,0);}for($i=0;$i<4;$i++)echo p($s($b,$p=122*$i,32),8)."-".p($s($b,$p+32,16),4)."-4".p($s($b,$p+48,12),3)."-".p("10".$s($b,$p+60,14),4)."-".p($s($b,$p+74,48),12)."\n";

Some output

> php 32309.min.php
> php 32309.min.php
> php 32309.min.php
> php 32309.min.php


$r = openssl_random_pseudo_bytes(61);
$b = "";
$s = "substr";
for($i=0 ; $i<61 ;) $b .= str_pad(decbin(ord($r[$i++])), 8, 0, 0);

function p($B, $L) {
    return str_pad(dechex(bindec($B)), $L, 0, 0);

for($i=0 ; $i<4 ; $i++) {
    echo p($s($b, 122*$i, 32), 8)."-";
    echo p($s($b, 122*$i+32, 16), 4)."-4";
    echo p($s($b, 122*$i+48, 12), 3)."-";
    echo p("10".$s($b, 122*$i+60, 14), 4)."-";
    echo p($s($b, 122*$i+74, 48), 12)."\n";
  • 3
    \$\begingroup\$ openssl_random_pseudo_bytes? That's.. verbose. PHP keeps amazing me - in the wrong way. \$\endgroup\$
    – seequ
    Jun 23, 2014 at 17:26

J - 114 bytes

I took the simplest approach available. The function takes amount of GUID's required (you could replace the ] near the end with 4: to make it return 4 always) and outputs that many of them, separated by newlines.

Edit: I missed that no generated bits must go to waste... This answer doesn't comply and I don't know how to make it to.


If an extra newline is allowed after the output, this could be written in 111 bytes.



   f 4
   f 5
  • \$\begingroup\$ @billpg This version generates a 32-bit integer for each necessary hexadecimal digit (for everything but the 4, - and linefeed). Is that okay? \$\endgroup\$
    – seequ
    Jun 23, 2014 at 17:19

Golfscript, 75

Fixed now!


Output example:

>ruby golfscript.rb p.gs

>ruby golfscript.rb p.gs

You can test it yourself here.

  • \$\begingroup\$ That bug is pretty. \$\endgroup\$
    – seequ
    Jun 23, 2014 at 18:33
  • \$\begingroup\$ Was pretty atleast. \$\endgroup\$
    – seequ
    Jun 23, 2014 at 19:02
  • \$\begingroup\$ Is "rand" in GS a cryptographic quality random number generator? \$\endgroup\$
    – billpg
    Jun 27, 2014 at 8:22

Python, 287 bytes

from random import choice
def gen4():
  for i in range(4):
    l = []
    d = choice(s[8:12])
    for k in range(30):

There's already a Python answer, but that just used .uuid, so here's one without it. Wish i could get it shorter though.

Still beat PHP :P


Python (85)

import os,uuid
for i in [1]*4:print uuid.UUID(os.urandom(16).encode('hex'),version=4)

os.urandom(n): Return a string of n random bytes suitable for cryptographic use.

I assume generating more than 488 bits is ok?

  • 1
    \$\begingroup\$ Standard library function is part of the library that comes with your language. os and uuid both are part of it. \$\endgroup\$
    – seequ
    Jun 23, 2014 at 15:48
  • 1
    \$\begingroup\$ I added the "Don't waste any bits" rule because I wanted to see some bit fiddling. Just being able to drop in the 4 wiping out other bits seemed too simple a task. \$\endgroup\$
    – billpg
    Jun 23, 2014 at 16:25
  • \$\begingroup\$ That's also why I specified four GUIDs to be output, so you'd have a whole number of random bytes to deal with. \$\endgroup\$
    – billpg
    Jun 23, 2014 at 16:29

Python 2 - 188 / 182

This should obey the requirements faithfully:

import os
a=(ord(x)>>i&3for i in(0,2,4,6)for x in os.urandom(61))
h=lambda n:eval('"%X"%(a.next()*4+a.next())+'*n+'"-"')
exec'print h(8)+h(4)+"4"+h(3)+"%X"%(8+a.next())+h(3)+h(12)[:-1];'*4

If python's UUID is acceptable for outputting a nicely-formatted uuid from a 32-digit hex string, then this version has 182 bytes:

import os,uuid
a=(ord(x)>>i&3for i in(0,2,4,6)for x in os.urandom(61))
exec('s='+'"%X"%(a.next()*4+a.next())+'*30+'"";print uuid.UUID(s[3:15]+"4"+s[:3]+"%X"%(8+a.next())+s[15:]);')*4

a is a generator comprehension that gives 2 bits at a time from an initial source of 61 random bytes.

Thanks TheRare for tips

  • \$\begingroup\$ You don't need to set x in t, just return it. for i in range(n):s+="%X"%(t()*4+t()) could be written exec's+="%X"%(t()*4+t());'*n and thus function h could be written in one line. You can use the same trick in the following loop too, just replace n by 4. \$\endgroup\$
    – seequ
    Jun 23, 2014 at 19:01
  • \$\begingroup\$ Whoops, yes you do need to set it. Sorry! The rest still works though. \$\endgroup\$
    – seequ
    Jun 23, 2014 at 19:08
  • \$\begingroup\$ @TheRare exec also seems to interfere with global, but still there is something to gain, thanks for reminding me about it \$\endgroup\$ Jun 23, 2014 at 19:10
  • \$\begingroup\$ Ah, sorry. I completely forgot that global causes it to break. What if you changed it to something like this (didn't check if it's shorted, but global s is not needed): def h(n):s='';exec's+="%X"%(t()*4+t());'*4;return s+'-'<LINEFEED HERE>print'\n'.join(h(8)+h(4)+'4'+h(3)+"%X"%(8+t())+h(3)+h(12)for _ in 1,2,3,4) \$\endgroup\$
    – seequ
    Jun 23, 2014 at 19:17
  • \$\begingroup\$ Or even h=lambda n:''.join("%X"%(t()*4+t())for _ in[0]*n)+'-';print'\n'.join(h(8)+h(4)+'4'+h(3)+"%X"%(8+t())+h(3)+h(12)for _ in[0]*4) (both are untested) \$\endgroup\$
    – seequ
    Jun 23, 2014 at 19:21

Common Lisp

SBCL, 223

int))(labels((g(n)(a(expt 2 n)))(h()(g 16)))(dotimes(i 4)(format
16384(g 12))(+ 32768(g 14))(h)(h)(h))))

CLISP, 299

(use-package :ffi)(def-call-out
a(:name"arc4random_uniform")(:arguments(u int))(:return-type
int)(:language :c)(:library :default))(labels((g(n)(a(expt 2
n)))(h()(g 16)))(dotimes(i 4)(format
16384(g 12))(+ 32768(g 14))(h)(h)(h))))

About arc4random_uniform(3)

Both of these call the C function arc4random_uniform(3) in BSD libc. Some systems do not have this function.

Beware that my declaration of arc4random_uniform(3) is wrong. The correct declaration is

u_int32_t arc4random_uniform(u_int32_t);

where u_int32_t is an old BSDism for an unsigned 32-bit integer. (The standard type in C99 is uint32_t without the first underscore.) My wrong declaration is

int arc4random_uniform(int);

where int is a signed 32-bit integer. (I know systems where int only has 16 bits, but I would not run Lisp there.) I am wrong to use signed for unsigned, but my program works because I only pass small non-negative integers. In code golf, int is shorter than unsigned-int in SBCL or uint in CLISP.

To use arc4random_uniform(3) in Lisp, I define a Lisp function a. Here it is in SBCL:

; (use-package :sb-alien)  ; SBCL does this by default!
(define-alien-routine ("arc4random_uniform" a)
  int       ; return type
  (u int))  ; argument named u

And in CLISP:

(use-package :ffi)
(def-call-out a
  (:name "arc4random_uniform")
  (:arguments (u int))
  (:return-type int)
  (:language :c)
  (:library :default))

CLISP has two languages, :c for traditional K&R C and :stdc for C89, but I find no difference, so I use the shorter name.

Now (a u) returns a random integer at least 0 and less than u. If u is 2n, then I get exactly n bits. For example, (a (expt 2 14) provides 14 bits. Because of my wrong declaration, u must be less than 231. If I want 32 bits, I may call (a 16) twice.

Rest of program

; after defining A
(labels ((g (n) (a (expt 2 n)))
         (h () (g 16)))
  (dotimes (i 4)
    (format t
            (h) (h) (h) (+ 16384 (g 12)) (+ 32768 (g 14)) (h) (h) (h))))

I define two local functions. (g n) generates n random bits. (expt 2 n) is much shorter than a left shift: 1<<n in C would become (let((v 0))(setf(ldb(byte 1 n)v)1)v) in Common Lisp!

(h) is an alias for (g 16). This alias costs 13 characters: 11 in (h()(g 16)), plus 2 to upgrade flet to labels, because h needs g in scope. Each call to (h) saves 3 characters, and I have 6 calls, so I save 18 after I spend 13.

When formatting each GUID, (+ 16384(g 12)) is shorter than (logior #x4000 (g 12) and (+ 32768(g 14)) is shorter than (logior #x8000 (g 14)).

One can generate the long format string "~4,'0X~4,'0X-~4,'0X-~4,'0X-~4,'0X-~4,'0X~4,'0X~4,'0X~%" (56 characters) with the expression (format()"~{~A~}~~%"(mapcar(lambda(e)(if e(format()"~~4,'0X")'-))'(t t()t()t()t()t t t))) (89 characters), but the literal string is shorter.


Javascript (ES6) - 177 163 158

Removed .toUpperCase(), replaced substring with substr and substr(0,3) to substr(1)

function f(){g=()=>(((1+Math.random())*0x10000)|0).toString(16).substr(1);for(i=0;i<4;i++)alert((g()+g()+"-"+g()+"-4"+g().substr(1)+"-"+g()+"-"+g()+g()+g()))}

EcmaScript 5 version of the above script:

function f(){function g(){return (((1+Math.random())*0x10000)|0).toString(16).substr(1)}for(i=0;i<4;i++)alert((g()+g()+"-"+g()+"-4"+g().substr(1)+"-"+g()+"-"+g()+g()+g()))}
  • \$\begingroup\$ Doesn't f=x=> work as the function definition? I've never used ES6 though. \$\endgroup\$
    – seequ
    Jun 24, 2014 at 11:51
  • \$\begingroup\$ @TheRare Nope.. \$\endgroup\$
    – Spedwards
    Jun 25, 2014 at 10:34

05AB1E, 41 bytes


Try it online.


4F                  # Loop 4 times:
  30F               #  Inner loop 30 times:
     15Ý            #   Push the list [0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15]
        Ω           #   Pop and push a random integer from this list
         h          #   Convert the integer to hexadecimal
          }J        #  After the inner loop: join the characters on the stack together
•2,ÈĆ•              #  Push compressed integer 40106286
      13в           #  Convert it to base-13 as list: [8,4,0,3,0,3,12]
         £          #  Split the string into parts of that size
                    #   i.e. "FBA4C1C6FE22479659019DA439B7D6"
                    #    → ["FBA4C1C6","FE22","","479","","659","019DA439B7D6"]
'-4и               '#  Push a list of 4 "-"s: ["-","-","-","-"]
    4               #  Push a 4
     DL             #  Duplicate it to create the list [1,2,3,4]
       7+           #  Add 7 to each: [8,9,10,11]
         h          #  Convert each to hexadecimal: [8,9,"A","B"]
          Ω         #  Pop and push a random value from this list
                    #   i.e. "B"
           ‚        #  Pair it together with the 4
            .ι      #  Interleave it with the list of "-": ["-",4,"-","B","-","-"]
              Á     #  Rotate it once towards the right: ["-","-",4,"-","B","-"]
               .ι   #  Interleave it with the earlier created list
                    #   → ["FBA4C1C6","-","FE22","-","",4,"479","-","","B","659","-","019DA439B7D6"]
                 J  #  Join this list together to a string
                    #   → "FBA4C1C6-FE22-4479-B659-019DA439B7D6"
                  , #  And output it with trailing newline

See this 05AB1E tip of mine (sections How to compress large integers? and How to compress integer lists?) to understand why •2,ÈĆ• is 40106286 and •2,ÈĆ•13в is [8,4,0,3,0,3,12].

Without the requirement that no random bits must go to waste, this could have been 36 bytes, by utilizing the builtin ǝ (based on my answer here):


Try it online.


Coconut 1.4.1, 114 bytes

from random import*
for C in[->f'{randrange(16**_):0{_}x}']*4:print(C 8,C 4,'4'+C 3,choice'89ab'+C 3,C 12,sep='-')

Try it online!

This works on the old version on TIO, for newer versions you might need parentheses around '89ab'.


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.