Random CSS Color code

10

3

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?

Mohsen

Posted 2013-09-16T21:14:06.277

Reputation: 677

Yours is broken, you're missing a + after the '#' – Doorknob – 2013-09-16T22:29:35.637

1You can remove the slice by doing this #'+(Math.random()*0xffffff|0).toString(16) – Griffin – 2013-09-17T10:39:19.183

1What is the "0xffffff" needed for? I'm not seeing much of a difference in my results without it. – path411 – 2013-09-17T22:18:21.007

4@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 – 2013-09-18T08:51:11.067

Answers

26

PHP 23 bytes

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

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

primo

Posted 2013-09-16T21:14:06.277

Reputation: 30 891

7This is one of my favourite solutions on the site. – Griffin – 2013-09-17T10:08:09.960

7

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"

Doorknob

Posted 2013-09-16T21:14:06.277

Reputation: 68 138

You don't need parentheses for method calls in Ruby, right? – Mohsen – 2013-09-16T22:50:07.730

@Mohsen Yes, but Ruby gets confused and thinks I'm calling to_s on 4095 if I omit them. – Doorknob – 2013-09-16T22:51:08.523

You can get it down to 20 with a sprintf format string: '#%06x'%rand(8**8-1) – Paul Prestidge – 2013-09-17T00:04:41.837

@chron Nice, thanks! Editing – Doorknob – 2013-09-17T00:05:25.623

6

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>

tristin

Posted 2013-09-16T21:14:06.277

Reputation: 189

1I'm not sure why this was downvoted. It works fine in every browser I've tested. +1 – primo – 2013-09-17T06:11:44.793

5#'+Math.random().toString(16).slice(-6) – Mohsen – 2013-09-17T22:30:40.383

@Mohsen nice! Didn't know slice could do negatives. – tristin – 2013-09-18T01:08:55.827

3

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

Cruncher

Posted 2013-09-16T21:14:06.277

Reputation: 2 135

1

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

marinus

Posted 2013-09-16T21:14:06.277

Reputation: 30 224

6?16 is 6 non-repeating random values though, that narrows the colorspace a bit... – mniip – 2014-02-17T15:27:45.797

1

Bash (51)

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

raspi

Posted 2013-09-16T21:14:06.277

Reputation: 121

0

Python 3, 53 bytes

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

Try it online!

Matthew Jensen

Posted 2013-09-16T21:14:06.277

Reputation: 713