By generating random images you can display a unique page to each of your visitors instead of showing them all the same thing. To display randomly generated images, you need to know how to generate random numbers and work with those numbers.
Random image generation algorithm
- Create an array with each item in the array being the name of an image.
- Declare a variable which will take the value of a randomly generated number from 0 (because arrays start at 0) to the length of the array of the images - 1. So in an array with 5 images, the variable will take the value of a randomly generated number between 0 and 4.
- Display the image from the images array at the index that matches the random number. So if the random number generated is 2, we will display the image at index 2. If the random number generated is 4, we will display the image at index 4, and so on.
Every time you reload this page new images will appear for both examples. Try it out!