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?
+
after the'#'
\$\endgroup\$ – Doorknob♦ Sep 16 '13 at 22:29#'+(Math.random()*0xffffff|0).toString(16)
\$\endgroup\$ – Griffin Sep 17 '13 at 10:39Math.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