Mr Speaker

#Random Hex colour

Want a random colour between white and black?

"#" + (Math.random() * 0xffffff | 0).toString(16)

That is all.

[Update: after I posted this I thought... i bet this has been done a zillion times already. It was, and by Paul Irish in 2009 no less. Additionally - it doesn't work. The best looking one there was:

'#'+('00000'+(Math.random()*(1<<24)|0).toString(16)).slice(-6);

3 Comments

  1. Alternatively, you can use the css3 rgb(x, x, x) or rgba(x, x, x, y) where x are random numbers between 0,255 and you is 0.1,0.9. Can’t type the actually code r easily on my phone, but that’s how I do it.

    Wednesday, July 24, 2013 at 5:13 pm | Permalink
  2. If you can do 3 random numbers between 0 and 255 in less than 63 characters… I wanna see the code!

    Wednesday, July 24, 2013 at 7:12 pm | Permalink
  3. I stumbled upon this nice piece of code and was wondering that one could achieve any tone between white and black by randomizing only values between 0.0 … 1.0 and have a color of black, alpha set to random variable? short enough? :)

    Monday, June 9, 2014 at 10:10 am | Permalink
Captcha! Please type 'radical' here: *
How did you find this thingo? *