Generate a random number between
65280
(
0x00ff00
) and
16711680
(
0xff0000
).
Math.random() - JavaScript | MDN[
^]
function getRandomInt(min, max) {
min = Math.ceil(min);
max = Math.floor(max);
return Math.floor(Math.random() * (max - min) + min);
}
const minValue = 0x00ff00;
const maxValue = 0xff0000;
let randomValue = getRandomInt(minValue, maxValue + 1);
Convert that number to hexadecimal:
let randomColor = "#" + randomValue.toString(16);
And that's your random colour.
Demo[
^]