9
\$\begingroup\$

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:

'#'+(Math.random()*0xffffff).toString(16).slice(-6)

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

Can we do better? What about other languages?

\$\endgroup\$
  • \$\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
24
\$\begingroup\$

PHP 23 bytes

#<?=md5(rand())&ÿÿÿÿÿÿ;

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

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

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

Ruby, 24 23 22 18

'#%03x'%rand(4095)

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

'#%06x'%rand(8**8-1)

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)

"#a83"

I assure you, I generated it randomly!

An even cheatier version:

"red"
\$\endgroup\$
  • \$\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
4
\$\begingroup\$

Javascript

'#'+Math.random().toString(16).substr(2,6)

Just a little shorter at 42.

\$\endgroup\$
  • 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
3
\$\begingroup\$

Fish 79

vnnnnnnnn 
601234567;
>xxxxxxxx<
 89""""""?
 nnABCDEF:
 vv""""""-
 vvoooooo1
 >>>>>>>>^

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.

Outputs:

python fish.py randomColor.fish
07FFF7

python fish.py randomColor.fish
07EFD7

python fish.py randomColor.fish
366F67

python fish.py randomColor.fish
977FD7

python fish.py randomColor.fish
97F7F7

python fish.py randomColor.fish
87F6FF
\$\endgroup\$
1
\$\begingroup\$

APL (17)

'#',(⎕D,⎕A)[6?16]

Explanation:

  • 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
\$\endgroup\$
  • \$\begingroup\$ 6?16 is 6 non-repeating random values though, that narrows the colorspace a bit... \$\endgroup\$ – mniip Feb 17 '14 at 15:27
1
\$\begingroup\$

Bash (51)

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

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.