devxlogo

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:

 
See also  Professionalism Starts in Your Inbox: Keys to Presenting Your Best Self in Email
devxblackblue

About Our Editorial Process

At DevX, we’re dedicated to tech entrepreneurship. Our team closely follows industry shifts, new products, AI breakthroughs, technology trends, and funding announcements. Articles undergo thorough editing to ensure accuracy and clarity, reflecting DevX’s style and supporting entrepreneurs in the tech sphere.

See our full editorial policy.

About Our Journalist