As George said the best way is to use HSL, so you can generate a bunch of random human-distinguishable colours. The similar idea is implemented in Adams Cole answer to the similar question, but his code have random color generator and hsl->hex rgb translator bundled together which makes it hard to understand and modify.
If you use one of the javascript color manipulation libraries (like jquery-color) color generation become trivial:
function rainbow() {
// 30 random hues with step of 12 degrees
var hue = Math.floor(Math.random() * 30) * 12;
return $.Color({
hue: hue,
saturation: 0.9,
lightness: 0.6,
alpha: 1
}).toHexString();
};