This blog post about generating random CSS color codes in JavaScript have multiple solutions for generating a random color in JavaScript. The shortest I can find is this:


If you are not familiar with CSS color code read documentations here.

Can we do better? What about other languages?

  • \$\begingroup\$ Yours is broken, you're missing a + after the '#' \$\endgroup\$ – Doorknob Sep 16 '13 at 22:29
  • 1
    \$\begingroup\$ You can remove the slice by doing this #'+(Math.random()*0xffffff|0).toString(16) \$\endgroup\$ – Griffin Sep 17 '13 at 10:39
  • 1
    \$\begingroup\$ What is the "0xffffff" needed for? I'm not seeing much of a difference in my results without it. \$\endgroup\$ – path411 Sep 17 '13 at 22:18
  • 4
    \$\begingroup\$ @path411 Theoretically, Math.random().toString(16) can produce a representation with less than 6 hex-digits after the (hexa)decimal point, in which case the function would break. For example, 0.1658172607421875 becomes 0.2A73 in hex. \$\endgroup\$ – primo Sep 18 '13 at 8:51

PHP 23 bytes


Where ÿ is character 255. Bitwise and will truncate the string returned from md5, which is already in hexadecimal format.

  • 7
    \$\begingroup\$ This is one of my favourite solutions on the site. \$\endgroup\$ – Griffin Sep 17 '13 at 10:08

Three character codes are valid too, so I can save some chars (4095 == 0xfff):

Ruby, 24 23 22 18


If I have to use a 6-char one, then:

Ruby, 28 27 26 24 20

Shaved one character off because 8**8-1 == 0xffffff


Thanks to chron for the format string, saving 4 chars!

Cheating (with this xkcd strip in mind):

Ruby/JS/Python/Perl/lots more, 6 (or 5)


I assure you, I generated it randomly!

An even cheatier version:

  • \$\begingroup\$ You don't need parentheses for method calls in Ruby, right? \$\endgroup\$ – Mohsen Sep 16 '13 at 22:50
  • \$\begingroup\$ @Mohsen Yes, but Ruby gets confused and thinks I'm calling to_s on 4095 if I omit them. \$\endgroup\$ – Doorknob Sep 16 '13 at 22:51
  • \$\begingroup\$ You can get it down to 20 with a sprintf format string: '#%06x'%rand(8**8-1) \$\endgroup\$ – Paul Prestidge Sep 17 '13 at 0:04
  • \$\begingroup\$ @chron Nice, thanks! Editing \$\endgroup\$ – Doorknob Sep 17 '13 at 0:05



Just a little shorter at 42.

  • 1
    \$\begingroup\$ I'm not sure why this was downvoted. It works fine in every browser I've tested. +1 \$\endgroup\$ – primo Sep 17 '13 at 6:11
  • 3
    \$\begingroup\$ #'+Math.random().toString(16).slice(-6) \$\endgroup\$ – Mohsen Sep 17 '13 at 22:30
  • \$\begingroup\$ @Mohsen nice! Didn't know slice could do negatives. \$\endgroup\$ – tristin Sep 18 '13 at 1:08

Fish 79


Not the shortest solution in the world, but it was fun to code :)

It's also not a uniform distribution, but all outputs have a non-zero probability 7 and F are most likely digits.


python fish.py randomColor.fish

python fish.py randomColor.fish

python fish.py randomColor.fish

python fish.py randomColor.fish

python fish.py randomColor.fish

python fish.py randomColor.fish

APL (17)



  • 6?16: 6 random numbers from 1 to 16
  • ⎕D,⎕A: the digits (0..9) followed by the alphabet (A..Z) (but only the first 16 values are ever used, i.e. 0..F)
  • '#',: add a # to the front
  • \$\begingroup\$ 6?16 is 6 non-repeating random values though, that narrows the colorspace a bit... \$\endgroup\$ – mniip Feb 17 '14 at 15:27

Bash (51)

od -N4 -An -tx /dev/urandom | cut -c2-7 | sed s/^/#/

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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