devxlogo

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:

 

Charlie has over a decade of experience in website administration and technology management. As the site admin, he oversees all technical aspects of running a high-traffic online platform, ensuring optimal performance, security, and user experience.

See also  How Seasoned Architects Evaluate New Tech

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.