Random CSS Color code
PHP 23 bytes
#<?=md5(rand())&ÿÿÿÿÿÿ;
Where ÿ
is character 255. Bitwise and will truncate the string returned from md5
, which is already in hexadecimal format.
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"
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>