Randomly Generating Multiple Images

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • adetamor
    New Member
    • Feb 2008
    • 4

    Randomly Generating Multiple Images

    I need a script that will randomly generate three images from an array, but they can't be the same image. I have a tried a few things, but I am just not having any luck. If anyone can think of anything that would make this work, I would appreciate it.
  • vee10
    New Member
    • Oct 2006
    • 141

    #2
    Hi,

    Try out with this it is giving differnet random numbers

    Code:
    function ImageSelection()
        {
       var images=new Array(3);
       images[0]="purple-sm.jpg";
       images[1]="redrose-sm.jpg";
       images[2]="whtrose.jpg";
        var i = Math.floor ( Math.random ( ) * 3 + 1 );
       // alert(i);
       document.getElementById("image1").src=images[parseInt(i)-1];
        }

    try out this .....

    Originally posted by adetamor
    I need a script that will randomly generate three images from an array, but they can't be the same image. I have a tried a few things, but I am just not having any luck. If anyone can think of anything that would make this work, I would appreciate it.

    Comment

    • adetamor
      New Member
      • Feb 2008
      • 4

      #3
      It appears I figured out a way to make it work. its pretty ugly, but it does exactly what it needs to. It creates 3 variables and uses the math.random to assign it a number within the boundaries of the array. Then it validates all the number to make sure they aren't the same, aren't zero and can't exceed the upper value of the array.

      Code:
      <script language="JavaScript">
      <!--
      
      
      function random_imglink(){
        var myimages=new Array()
      //specify random images below. You can have as many as you wish
        myimages[1]="img1.gif"
        myimages[2]="img2.gif"
        myimages[3]="img3.gif"
      
      //specify corresponding links below
        var imagelinks=new Array()
        imagelinks[1]="http://www.thescripts.com"
        imagelinks[2]="http://www.thescripts.com"
        imagelinks[3]="http://www.thescripts.com"
      
        var ry=Math.floor(Math.random()*myimages.length)
        var rx=Math.floor(Math.random()*myimages.length)
        var rz=Math.floor(Math.random()*myimages.length)
        
      //This shows the values of the variables before validating
      //Omit this code when actually using  
        document.write('The value of var RY is ' + ry + '<br>')
        document.write('The value of var RX is ' + rx + '<br>')
        document.write('The value of var RZ is ' + rz + '<br>')
      //End Omit
        
      //change >= value to the second highest number in your array
      //this will keep RX and RZ from going past the highest number in your array 
        if (ry>=2)
           ry=ry - 2
        if (ry==0)
           ry=1
      //This shows the value of the variable after validating
      //Omit the following line when actually using  
      	 document.write('The value of var RY after validating is ' + ry + '<br>')
           document.write('<a href='+'"'+imagelinks[ry]+'"'+'><img src="'+myimages[ry]+'" border=0></a><br>')
      
      //change == value to the highest number in your array
      //this will keep RZ from going past the highest number in your array 
        if (rx==3)
        	 rx=rx - 1
        if (rx==0)
        	 rx=1
      //This keeps from duplicating variable values
        if (rx==ry)
           rx=ry + 1
      //This shows the value of the variable after validating
      //Omit the following line when actually using 
      	 document.write('The value of var RX after validating is ' + rx + '<br>')
      	 document.write('<a href='+'"'+imagelinks[rx]+'"'+'><img src="'+myimages[rx]+'" border=0></a><br>')
      
        if (rz==0)
           rz=1
      //This keeps from duplicating variable values
        if (rz==ry)
      //This keeps from duplicating variable values  
           rz=ry + 1
        if (rz==rx)
        	 rz=rx + 1
      //This shows the value of the variable after validating
      //Omit the following line when actually using 
      	 document.write('The value of var RZ after validating is ' + rz + '<br>')
      	 document.write('<a href='+'"'+imagelinks[rz]+'"'+'><img src="'+myimages[rz]+'" border=0></a>')
      }
        random_imglink()
      //-->
      </script>

      Comment

      Working...