473,397 Members | 2,033 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,397 software developers and data experts.

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 1428
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

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

Similar topics

4
by: Nathan Given | last post by:
Hello All, I am trying to randomly change the background image of my home page but I can't seem to figure it out. Here is a snippet of my css .... BODY {background:transparent...
0
by: misscrf | last post by:
I am currently working on a database, in 3rd normal form, which is for candidates who apply for a job with the law firm that I workd for. My issue is with good form design. I have a main...
4
by: James | last post by:
Just learning C#. What's the easiest way to assign numbers 1-10 randomly to an array? Basically I just want to take the numbers 1-10 and arrange them randomly in slots 0-9 of an array. Thanks
0
by: Ryan Harvey | last post by:
Hi all, I have written a web user control that contains a repeater control. the ItemTemplate for this control is basically 6 Hyperlinks in a row, that are dynamically allocated one of 7 gif...
8
by: Lars Netzel | last post by:
Hey! I wrote a message yersterday and got some good answers and have now managed to generate a nice image. The problem is that it's all generated in an Images.Aspx file and I don't really want...
3
by: Arun | last post by:
Hi, I have simple question to ask. How to write multiple Binary files to the Browser using Asp.Net and Visual C#.net I have seen examples where single binary file is written to browser. ...
1
by: Xah Lee | last post by:
The following is a program to generate thumbnail images for a website. Useful, if you want to do that. It is used to generate the thumbnails for my “Banners, Damsels, and Mores” project...
2
by: bizt | last post by:
Hi, Is it possible to obtain the width/ height of an image when that image is dyanically created using a PHP script and passing GET attributes. For example: <img...
0
by: Miki | last post by:
Hello, I'm trying to write something like http://blabberize.com/, generating a video file with lip-sync. Currently the general idea is: * Generate animation images using GIMP * Sample voice...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.