I’ve been playing around with <canvas> lately and have had a frequent need to generate random colours. I have written my own functions for doing this, which I now use all the time. The first function below generates a simple array of RGB values, while the second snippet generates a hexadecimal string. Generating RGB Values The snippet for generating RGB values is quite simple. We use the Math.random() function to generate a 3-element Array of whole numbers between 0 and 255.