469,314 Members | 2,197 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,314 developers. It's quick & easy.

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.
Feb 25 '08 #1
2 1331
vee10
141 100+
Hi,

Try out with this it is giving differnet random numbers

Expand|Select|Wrap|Line Numbers
  1.  
  2. function ImageSelection()
  3.     {
  4.    var images=new Array(3);
  5.    images[0]="purple-sm.jpg";
  6.    images[1]="redrose-sm.jpg";
  7.    images[2]="whtrose.jpg";
  8.     var i = Math.floor ( Math.random ( ) * 3 + 1 );
  9.    // alert(i);
  10.    document.getElementById("image1").src=images[parseInt(i)-1];
  11.     }
  12.  
  13.  

try out this .....

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.
Feb 25 '08 #2
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.

Expand|Select|Wrap|Line Numbers
  1. <script language="JavaScript">
  2. <!--
  3.  
  4.  
  5. function random_imglink(){
  6.   var myimages=new Array()
  7. //specify random images below. You can have as many as you wish
  8.   myimages[1]="img1.gif"
  9.   myimages[2]="img2.gif"
  10.   myimages[3]="img3.gif"
  11.  
  12. //specify corresponding links below
  13.   var imagelinks=new Array()
  14.   imagelinks[1]="http://www.thescripts.com"
  15.   imagelinks[2]="http://www.thescripts.com"
  16.   imagelinks[3]="http://www.thescripts.com"
  17.  
  18.   var ry=Math.floor(Math.random()*myimages.length)
  19.   var rx=Math.floor(Math.random()*myimages.length)
  20.   var rz=Math.floor(Math.random()*myimages.length)
  21.  
  22. //This shows the values of the variables before validating
  23. //Omit this code when actually using  
  24.   document.write('The value of var RY is ' + ry + '<br>')
  25.   document.write('The value of var RX is ' + rx + '<br>')
  26.   document.write('The value of var RZ is ' + rz + '<br>')
  27. //End Omit
  28.  
  29. //change >= value to the second highest number in your array
  30. //this will keep RX and RZ from going past the highest number in your array 
  31.   if (ry>=2)
  32.      ry=ry - 2
  33.   if (ry==0)
  34.      ry=1
  35. //This shows the value of the variable after validating
  36. //Omit the following line when actually using  
  37.      document.write('The value of var RY after validating is ' + ry + '<br>')
  38.      document.write('<a href='+'"'+imagelinks[ry]+'"'+'><img src="'+myimages[ry]+'" border=0></a><br>')
  39.  
  40. //change == value to the highest number in your array
  41. //this will keep RZ from going past the highest number in your array 
  42.   if (rx==3)
  43.        rx=rx - 1
  44.   if (rx==0)
  45.        rx=1
  46. //This keeps from duplicating variable values
  47.   if (rx==ry)
  48.      rx=ry + 1
  49. //This shows the value of the variable after validating
  50. //Omit the following line when actually using 
  51.      document.write('The value of var RX after validating is ' + rx + '<br>')
  52.      document.write('<a href='+'"'+imagelinks[rx]+'"'+'><img src="'+myimages[rx]+'" border=0></a><br>')
  53.  
  54.   if (rz==0)
  55.      rz=1
  56. //This keeps from duplicating variable values
  57.   if (rz==ry)
  58. //This keeps from duplicating variable values  
  59.      rz=ry + 1
  60.   if (rz==rx)
  61.        rz=rx + 1
  62. //This shows the value of the variable after validating
  63. //Omit the following line when actually using 
  64.      document.write('The value of var RZ after validating is ' + rz + '<br>')
  65.      document.write('<a href='+'"'+imagelinks[rz]+'"'+'><img src="'+myimages[rz]+'" border=0></a>')
  66. }
  67.   random_imglink()
  68. //-->
  69. </script>
Feb 25 '08 #3

Post your reply

Sign in to post your reply or Sign up for a free account.

Similar topics

4 posts views Thread by James | last post: by
reply views Thread by Ryan Harvey | last post: by
8 posts views Thread by Lars Netzel | last post: by
3 posts views Thread by Arun | last post: by
1 post views Thread by Xah Lee | last post: by
reply views Thread by Miki | last post: by
reply views Thread by zhoujie | last post: by
reply views Thread by harlem98 | last post: by
1 post views Thread by Geralt96 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.