Like the article? Be sure to subscribe to our RSS feed and follow us on Twitter to stay up on recent content.

Try to refresh the demo page to see the effect.

We will use javascript function Math.random() to generate a random number between 0 and number of elements of an array which is an arrangement of stylesheet items.

Create a new javascript file which contains this function:

function rand(){
    return Math.round(Math.random()*(css.length-1));
}

This gets us numbers from 0 to < (css.length-1) (Because the elements start from 0) which is this array below:

var css = new Array(
    '<link rel="stylesheet" type="text/css" href="default.css">',
    '<link rel="stylesheet" type="text/css" href="style2.css">',
    '<link rel="stylesheet" type="text/css" href="style3.css">',
    '<link rel="stylesheet" type="text/css" href="style4.css">'
);

Then write it in HTML

rand = rand();

document.write(css[rand]);

That’s it! Enjoy the random CSS by refresh your webpage. Your random effect will show clearly if you have many elements in array. The more elements, the less repeat.