# Random CSS Color code

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?

• Yours is broken, you're missing a + after the '#' – Doorknob Sep 16 '13 at 22:29
• You can remove the slice by doing this #'+(Math.random()*0xffffff|0).toString(16) – Griffin Sep 17 '13 at 10:39
• What is the "0xffffff" needed for? I'm not seeing much of a difference in my results without it. – path411 Sep 17 '13 at 22:18
• @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. – primo Sep 18 '13 at 8:51
• Linked from Random color generator. – Peter Mortensen Jan 12 at 3:01

## PHP 23 bytes

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


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

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

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

## Ruby, 242322 18

'#%03x'%rand(4095)


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

## Ruby, 28272624 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"

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

# Javascript

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


Just a little shorter at 42.

function randomColor() {
return '#' + Math.random().toString(16).substr(2, 6);
}

for (var n = 0; n < 16*9; n++) {
var el = document.createElement('SPAN');
el.style.backgroundColor = randomColor();
document.getElementById('demo').appendChild(el);
}
span { width: calc(100%/16);
height: calc(100vh/9);
margin-top: -7px;
display: inline-block;
}
<div id='demo'></div>

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

# 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


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

## Bash (51)

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


# Python 3, 53 bytes

from random import*;print(f"#{randint(0,16**6):06x}")


Try it online!