PDA

View Full Version : image randomizing script



gnossos
12-29-2003, 07:36 AM
hey all,
I tried using the script located here:
http://www.alistapart.com/articles/randomizer/
to change a banner image when the browser is refreshed, I've tested this on a different server and things work fine but when used on the westhost server, the image only changes every 3 - 7 refreshes when it should be changed every refresh. I'm not sure if there is something going on in the php or apache side that can be edited to fix this or .... ?
muchas gracias.

wildjokerdesign
12-29-2003, 08:38 AM
From looking at the script the only thing I see that randomizes the image is this

if (count($fileList) > 0) {
$imageNumber = time() % count($fileList);
$img = $folder.$fileList[$imageNumber];
}


It got the filelist from opening the directory and looking at all the images in it. The it looks like it takes the current time and divides (think that is right) it by the total count of files in the directory. So if anything it would be the time variable that would effect it not changing on every refresh. I can't think of anything you would change but thought I would post the bit of code that randomizes it in case anyone sees something.

I have a perl script that does the same thing but uses this bit of code to randomize

srand(time ^ $$);
my $i = rand(@random);


It works well although sometimes it well give the same image on a quick refresh but I assumed that was just the process of randomizeing at work. It look like it is using time as a randomizer also.

Just some thoughts and more info for folks to mull over.

jalal
12-29-2003, 09:13 AM
time() returns seconds, so unless you refresh more than once a second, that shouldn't be a problem.

Maybe your browser is caching the page?

wildjokerdesign
12-29-2003, 10:57 AM
I was just reading in my php book :) and noticed that it php can also use the srand and rand function. I could not figure out if perl and php use them the same way. Could the perl code work in the php code if @random was replaced by count($fileList) and would it be any better or reliable?

jalal
12-29-2003, 01:58 PM
I have random 'things' happening on quite a few sites, and I've always used:

srand( time() ); // to seed the generator
and
$i = rand( 0, MAX_NUM ); // to get a random number.

Never had a problem.