Random Image

Is there a way to display a random image? If so could someone tell me the code or point me in the way of a tutorial? thanks.

one thing you can do is have the images inside a movieclip, and have an actionscript like this assigned to it


onClipEvent(load){
frameNumber = Math.round(Math.random()*this._totalframes)
gotoAndPlay(frameNumber)
}

cheers

Hey ahmed, thanks for that, but actually I was looking for a non flash way to do it… possibly html or javascript?
sorry for not specifying

oops… im sure there’s a way to do it with javascript, but im not into js anyways…

do you mean, when ever the page loads, a new immage is there?

yup alex, thats what i mean:beam:

I will get right on it… brb. I know the script, but I want to write it and test it first just to be safe.

Wow, thanks a lot lost. (heh, that was alliteration)
I really appreciate it.
My site is gonna be soo cool:beam:



<HEAD>

<SCRIPT LANGUAGE="JavaScript">

<!-- Begin
// Set up the image files to be used.
var theImages = new Array() // do not change this
// To add more image files, continue with the
// pattern below, adding to the array.

theImages[0] = '1.gif'
theImages[1] = '2.gif'
theImages[2] = '3.gif'
theImages[3] = '4.gif'

// do not edit anything below this line

var j = 0
var p = theImages.length;
var preBuffer = new Array()
for (i = 0; i < p; i++){
   preBuffer* = new Image()
   preBuffer*.src = theImages*
}
var whichImage = Math.round(Math.random()*(p-1));
function showImage(){
document.write('<img src="'+theImages[whichImage]+'">');
}

//  End -->
</script>

</HEAD>

<!-- STEP TWO: Copy this code into the BODY of your HTML document  -->

<BODY>

<SCRIPT LANGUAGE="JavaScript">


<!-- Begin
showImage();
//  End -->
</script>

<SCRIPT LANGUAGE="JavaScript">
<!--
var imgArray = ["image1.gif", "image2.gif", "image3.jpg"];
var ranNum = Math.floor(Math.random()*imgArray.length);
document.write("<IMG SRC='"+imgArray[ranNum]+"' BORDER='0'>");
-->
</SCRIPT>

Just put that script in your HTML code where you want your image to appear at.

Wow alex, a bit advanced just for a random image. Why do all the extra work?

lol, i found it online.

Ahhhhh, explains a lot, well mine does the same but is written much simpler to do what it needs to do and just what it needs to do :wink: :beam:

yea, smaller code is always better. it also saves on space, and loading time.

Lost and Alex: Thank you guys soooo much. I don’t really have time to test it right now, but I’m sure it works, I mean it made sense to me, so why wouldnt it.

THANK YOU!!
if you guys wanna see what im usin it for go to the drawing forum and look under site desing.
thanks:beam:

For mine, if you want to add a few lines to make it a tad bit easier to read you could make it…

<SCRIPT LANGUAGE="JavaScript">
<!--
var imgArray = ["image1.gif", "image2.gif", "image3.jpg"];
var ranNum = Math.floor(Math.random()*imgArray.length);
var chosenImage = imgArray[ranNum];
var writeTag = "<IMG SRC='"+chosenImage+"' BORDER='0'>";
document.write(writeTag);
-->
</SCRIPT>

Which defines the array, then the random number variables. The it chooses the image from the array with the chosenImage variable. The writeTag variable then writes out the HTML IMG tag with the chosenImage variable as its src parameter.
Then we just use the javascript function to write out the writeTag variable.

It works lost!
I gotta say, that makes my site like 100000 times cooler. THANK YOU!

Lost, on that last post, I have no idea what you mean… lol.
Is that supposed to make it easier? cuz it just confused the hell outta me…
I think I’m missing something.

It does the same thing, but it “uncondenses” it to make it easier to read and understand if you knew anything about JS (or even if you knew AS you should understand this)

I know extremely basic AS and the first one made sense to me, thats how i wanted to make it, i just didnt know how.
I dont understand the write tag thing at all and what its purpose is. I think I’m happy with the first one.
I gotta say lost, you kick ***, thank you soo much for helping.

and i saw you viewing my thread in D&D… you didn’t respond:*(