Solved: javascript random color generator

The main problem with a random color generator is that it is not truly random. If two different colors are generated at the same time, then they will be close to each other in color.


var colors = ['#FF0000','#00FF00','#0000FF'];

var rand = Math.floor(Math.random()*colors.length);

document.bgColor = colors[rand];

This code line is setting the background color of a document to a random color from an array. The array has three colors in it, red, green, and blue. The code line first creates a variable called rand and sets it equal to a random number between 0 and 2. It then sets the background color of the document to the color in the colors array at the index that is equal to rand.

Working with colors in JavaScript

Working with colors in JavaScript can be a bit tricky. There are a few different ways to do it, and each has its own advantages and disadvantages.

One way to work with colors in JavaScript is to use the color property of the objects that you create. For example, you could create a red object like this:

var red = { color : ‘red’ };

You can then use the color property to access the color of the object, like this:

red . color ; // ‘red’

Random function

A random function in JavaScript is a function that returns a random number. This can be useful for generating unique numbers, or for creating randomized tests.

Related posts:

Leave a Comment