Random Image Display Using JavaScript
Look for those step below:
1. Initializing an array
2. Storing the image file names in that array
3. Finding the length of this array
4. Using the Math.random() to generate a random number
5. Using the randomly generated number as index for retrieving an image file name from the array.
6. Displaying the image.
var img_name = new Array("purple.gif", "red.gif",
"blue.gif", "yellow.gif", "green.gif", "pink.gif");
var l = img_name.length;
var rnd_no = Math.round((l-1)*Math.random());
document.r_img.src = img_name[rnd_no];

NAME="r_img" ALT="Random image">
Note: Array are zero-indexed, thus, we have to decrease the value of variable l (length of the array) by 1 when generating a random number.