JavaScript: Random Images using Math.random()

Video ready, click here to close ×


This is a tutorial for programming BEGINNERS.

This tutorial will show you how to use JavaScript: Math.random(). We will use Math.random to display images at random on a webpage.

source

9 comments

  • Hello ! Interesting video but can I generate 6 images on my page (for a card game for example…)? I'm beginner and not an english speaker…. I have a correct HTML/CSS theme, but JavaScript is a new world for me. I would like to create a basic program for my father who created a physical card game

    (with very easy rules…

    basicly there are 18 differents cards with their own value (1 -> 18), and I want to launch a random function to begin the game, a "onclick system" to add a clickable card (the player ones) in a <div> . It could be a very good start for me, and a very good training.
    )

    Receive my bottle in the web…. Internetly :)

  • Hi
    How can I use random image with question?
    Please help me 

  • u r using 1.jpg, 2.jpg,3.jpg but i want a script for different name of image files like
    ram1.jpg; ramu2.jpg; sdfj.jpg; jsjf.jpg can u randamize like these images

  • Can this be used for htmls rather than jpgs?

  • If you have an image folder for example called "example", you put the folder-name between the two ""-brackets, combined with a slash ("/"). So the line would look like this: "<img src= "example/"

  • Can you have this as a function so you can call it whenever a button is pressed? And does the javascript code need to be edited at all to do this? Thanks, great tutorial by the way :)

  • Hey can you help me please. All i want to do is generate random sentences i have specified in an array using a button 

  • Sure, I'll help you. Message me your existing code, please.

  • Awesome tutorial. I was wondering if you provide me how to declare the variable instead of what you have done, also I put my images in a folder. Could you help me out? Thanks.

Leave a Reply

Your email address will not be published. Required fields are marked *