PDA

View Full Version : Randomizer



Rayban
02-06-2005, 04:45 AM
Searched the forums and the site to "randomize" a set of pictures, have one picture change everytime the page is hit

Thank you!

cr3ative
02-06-2005, 12:14 PM
Something like this? http://www.jsmadeeasy.com/javascripts/Banners/Random%20Ads/template.htm

Otherwise, try www.google.com .

cr3ative

darco9x2
02-07-2005, 01:33 AM
<script language='javascript'>

//-- Generate a random number between 0 and max.
function randomNum(max) {
var rNum=NaN
while (isNaN(rNum)) {
rNum=Math.floor(Math.random()*(max))
}
return rNum
}
var pic = new Array()
pic[0]="shot01.jpg"
pic[1]="shot02.jpg"
pic[2]="shot03.jpg"
pic[3]="shot04.jpg"
pic[4]="shot05.jpg"
pic[5]="shot06.jpg"
pic[6]="shot07.jpg"
pic[7]="shot08.jpg"
pic[8]="shot09.jpg"
pic[9]="shot10.jpg"
pic[10]="shot11.jpg"
pic[11]="shot12.jpg"
pic[12]="shot13.jpg"
pic[13]="shot14.jpg"
pic[14]="shot15.jpg"
</script>
just rename the shotx.jpg

then

<script language='javascript'>
imgTag='<img border="0" src="'
imgTag+=pic[randomNum(pic.length)]
imgTag+='" align="left" hspace="0"'
imgTag+=' width="200" height="150"'
document.write (imgTag)
</script>
for the pic
change the align, width and all that crap. this should work, it works for me, tweak it if you have to.

mwinter
02-07-2005, 02:04 PM
<script language='javascript'>Please use up-to-date mark-up. Use of the language attribute should have ended over six years ago.


<script type="text/javascript">is sufficient in all cases.



while (isNaN(rNum)) {
rNum=Math.floor(Math.random()*(max))
}I'm curious: why do you think that will produce NaN? Unless the argument max is not a number, I don't see how that could happen. However, even then the code will go into an infinite loop so it should be the responsibility of the caller to make sure the argument is valid.


/* Generates a pseudo-random integer
* in the range o <= x < (n + o)
* where o defaults to zero (0).
*/
function random(n, o) {o = +o || 0;
return Math.floor((Math.random() % 1) * n) + o;
}

var pic = new Array()
[...]It's generally better to use an array literal to create an initialised array:


var pic = [
'shot01.jpeg',
'shot02.jpeg',
/* ... */
'shot15.jpeg'
];

imgTag='<img border="0"If you don't want borders on images, use CSS:


img {
border-style: none;
}It's far more efficient. Dump deprecated attributes like align and hspace. HTML 3.2 is dead.

Mike

darco9x2
02-08-2005, 03:04 AM
k thanks

cars-ten
07-09-2005, 03:18 PM
Hi everybody,
just found your thread and noticed that all of you got stuck on the idea of using an array for picking a random picture. Here is a (shorter) solution without having to set up and fill an array:


<script type="text/javascript">
function randpic(pic,n) {
// picks a random picture
// pic: path to pictures and first (common) part of picture name
// each picture carries a 3-digit number from 1 to n and ends with ".jpg"
var i=String(1000+Math.ceil(Math.random()*n)).substr(1)
document.writeln("<img src=\""+pic+i+".jpg\" alt=\"Bild Nr. "+i+"\">")
}
</script>