Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Random method 1

Status
Not open for further replies.

tseh

Programmer
Jan 13, 2000
64
CA
I have a series of picture that I want to show. One random picture per session. I don't quite understand the random method provided in JavaScript. Could some one explain or suggest another solution?<br>
<br>
Thanks
 
&nbsp;&nbsp;&nbsp;&nbsp;The JavaScript <FONT FACE=monospace>math.random()</font> function simply generates a random decimal number between 0 & 1, inclusive. To change this to an integer value to be used in selecting a random picture, simply multiply the number generated by the number of pictures you have, then convert to integer...then you can call <FONT FACE=monospace>document.</font>image_name<FONT FACE=monospace>.src=eval(randomNumber)</font><br>&nbsp;&nbsp;&nbsp;&nbsp;This method will change the source of the first picture on your page to whatever location is assigned to the variable whose name matches the value of <FONT FACE=monospace>randomNumber</font><br><br>Ex:<br><br><FONT FACE=monospace>&lt;script language=JavaScript&gt;&lt;!--;<br>var randomNumber=math.random()*5;<br><br>var 1=&quot;<A HREF=" TARGET="_new"> 2=&quot;<A HREF=" TARGET="_new"> 3=&quot;<A HREF=" TARGET="_new"> 4=&quot;<A HREF=" TARGET="_new"> 5=&quot;<A HREF=" TARGET="_new">[0].src=eval(randomNumber);<br>//--&gt;&lt;/script&gt;</font> <p>-Robherc<br><a href=mailto:robherc@netzero.net>robherc@netzero.net</a><br><a href= shared.freeservers.com/searchmaster.html>SearchMaster Interface...11-in-1</a><br>Wanting to learn Assembler; please e-mail me any tutorials or links for it that are useful to you :)
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top