<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Document</title>
</head>
<body>
<script>
var color = ['red', 'green', 'blue', 'yellow', 'black'], x;
(bgc =()=> {
do rnd = Math.floor(Math.random()*color.length);
while(rnd == x);
x = rnd;
document.body.style.backgroundColor = color[rnd];
setTimeout(bgc, 2000);
})();
</script>
</body>
</html>
если нужно без повторов