...

View Full Version : Making "Random Image" code more *Random*



kjs
05-24-2006, 02:01 AM
Hello all,
Ok so I've got this code to randomise the appearance of images on my site, but as the case stands, it doesn't actually do as I expected. Say I click refresh, all it does is go to the next image in the order in which I have listed them. Anyway, here's the code, and I was just wondering if any of you guys could help me to make it more spontaneous, and not just got from image 1 to image 2, then 3, then image 4 etc.

Thanks alot in advance everyone, here's the code:-


<script language="Javascript">

var currentdate = 0
var core = 0

function StringArray (n) {
this.length = n;
for (var i =1; i <= n; i++) {
this[i] = ' '

}
}

image = new StringArray(10)
image[0] = 'Images/CelebrityFish/anthonystewarthammerheadhea.jpg'
image[1] = 'Images/CelebrityFish/ashleeshrimpsonsimpson.jpg'
image[2] = 'Images/CelebrityFish/bananapirahnarama.jpg'
image[3] = 'Images/CelebrityFish/bennygillhill.jpg'
image[4] = 'Images/CelebrityFish/billgillmurray.jpg'
image[5] = 'Images/CelebrityFish/carlcarplewis.jpg'
image[6] = 'Images/CelebrityFish/cliffpilchardrichard.jpg'
image[7] = 'Images/CelebrityFish/davidgraylinggray.jpg'
image[8] = 'Images/CelebrityFish/dolphdolphinlundgren.jpg'
image[9] = 'Images/CelebrityFish/ericclamtunacantona.jpg'

var ran = 60/image.length

function ranimage() {
currentdate = new Date()
core = currentdate.getSeconds()
core = Math.floor(core/ran)
return(image[core])
}

document.write("<img src='" +ranimage()+ "'>")

</script>

Cheers, KJS.

boxxer03
05-24-2006, 07:10 AM
Try this:


<script type="text/javascript">
function randomimage(){
image = new Array()
image[0] = 'Images/CelebrityFish/anthonystewarthammerheadhea.jpg'
image[1] = 'Images/CelebrityFish/ashleeshrimpsonsimpson.jpg'
image[2] = 'Images/CelebrityFish/bananapirahnarama.jpg'
image[3] = 'Images/CelebrityFish/bennygillhill.jpg'
image[4] = 'Images/CelebrityFish/billgillmurray.jpg'
image[5] = 'Images/CelebrityFish/carlcarplewis.jpg'
image[6] = 'Images/CelebrityFish/cliffpilchardrichard.jpg'
image[7] = 'Images/CelebrityFish/davidgraylinggray.jpg'
image[8] = 'Images/CelebrityFish/dolphdolphinlundgren.jpg'
image[9] = 'Images/CelebrityFish/ericclamtunacantona.jpg'

var pickimage = Math.floor(Math.random()*image.length);
document.write('<img src="' +image[pickimage]+ '">');
}
randomimage();
</script>

vwphillips
05-24-2006, 08:45 AM
a cookie will be necessary to ensure that the last image is not selected again

kjs
05-24-2006, 10:47 PM
Legends. Thanks guys.

Boxxer, it worked a treat, and Phillips, thank you for the cautionary advice.

This place is a breath of fresh air - no idiots evident, and genuine, prompt and helpful advice.

Thanks alot!

boxxer03
05-25-2006, 01:18 AM
You don't necessarily need a cookie, you could always do add in a line or two that keeps track of what image it is currently on and when it goes to switch images have it check and make sure they're not the same picture.

edited: my bad, he's right you would need to use a cookie. for some reason i was thinking you were using a timer and rotating them that way. my bad.

kjs
05-25-2006, 01:37 AM
It's working fine as it is chaps.



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum