Show a Random Image

Show a Random Image

A bit of randomness can help keep your Web site from getting stale. You can use the JavaScript rand() function to display random text or a random image.

 function goRand() {var num = 3;  // number of imagesmyImages = new Array();myImages[0] = "image1.gif";myImages[1] = "image2.gif";myImages[2] = "image3.gif";rand = Math.floor(Math.random() * num);current_image = myImages[rand];}

The Math.random() function generates a random number between 0 and 1, and the Math.floor() function rounds it down to the nearest integer. Use the document.write() function to display the randomly generated image:



Share the Post: