SitePoint Sponsor

User Tag List

Results 1 to 2 of 2
  1. #1
    SitePoint Enthusiast
    Join Date
    Dec 2010
    Posts
    30
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    getElementbyId contn'd

    i have a script that outputs 4 random images from a folder:

    HTML Code:
    var imagenumber = 9 ;
    var randomnumber = Math.random() ;
    var rand1 = Math.round( (imagenumber-1) * randomnumber) + 1 ;
    
    var randomnumber2 = Math.random() ;
    var rand2 = Math.round( (imagenumber-1) * randomnumber2) + 1 ;
    
    var randomnumber3 = Math.random() ;
    var rand3 = Math.round( (imagenumber-1) * randomnumber3) + 1 ;
    
    var randomnumber4 = Math.random() ;
    var rand4 = Math.round( (imagenumber-1) * randomnumber4) + 1 ;
    the 4 random images generated are inserted into 4 seperate divs (<div id="highlight_1"><div id="highlight_2"><div id="highlight_3"><div id="highlight_4">.

    the question i have is, how can you make the output have each of the 4 images different from each other (no repeats on the page)? i always get duplicate random numbers. any ideas?

  2. #2
    SitePoint Wizard
    Join Date
    Oct 2005
    Posts
    1,849
    Mentioned
    5 Post(s)
    Tagged
    1 Thread(s)
    If you want to ensure uniqueness of your randomly generated numbers, the easiest way would be to assign the value to a variable or an array and then check on each subsequent generation if the number generated has been generated previously and if so, generate a new one until you get something unique.

    You have to track what you have already generated.


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •