Re: Random Images with no duplicates?

From: Date: Wed, 21 Dec 2005 02:57:05 +0000
Subject: Re: Random Images with no duplicates?
Groups: php.general 
Request: Send a blank email to php-general+get-227755@lists.php.net to get a copy of this message
"Mike" <hyatt65@hotmail.com> wrote in message news:... I'd just like to thank everyone who helped me our with this. I think it's great that so many people would be so helpful and not expect anything in return. This list is quite a storehouse of info and I'm learning just from reading the posts since I joined last week! Again, thanks all. -Mike > ""Kilbride, James"" <James.Kilbride@gd-ais.com> wrote in message > 4a¾Ñ%± > \z¸vArH*news:E0C1686B8F45434DA0E0A904672EA1A859C140@MAPF01-MAIL01.ad.gd-ais.com... > Actually with this idea all you do is reduce $max after each successful > pull and 'increment' over pictures you already have. That way you never > have to pull another random number. You just may have to increment > through the entire set of pulled images if you pulled a high enough > number. > > for($i = 0; $i < $want; $i++) { > $random = mt_rand(0,$max); > for($j = 0; $j < $i; $j++) { > if ($retrieved[$j] < $random) { > $random++; > } > } > $retrieved[] = $random; > sort($retrieved); > $max--; > } > > Guarentees unique choices. > > > >> > I'm still very green with PHP, but am working to fix that. >> > >> > I've searched high and low for an answer to this question >> and so far >> > no solution has presented itself. I have a script to >> populate my web >> > page with random images: >> > >> > <?php >> > #random images example >> > #this is your file >> > $file = "images.txt"; >> > #open the file >> > $openFile = file($file); >> > #generate a random number >> > srand((double)microtime()*1000000); >> > #get one of the entries in the file >> > $random_image = $openFile[array_rand($openFile)]; #display >> the entry >> > echo "<img src='$random_image' height='170' >> > width='100'></img>"; ?> >> > >> > >> > This works beautifully, but my problem is that sometimes, >> the same image >> > displays twice or more on the same page-load. My page >> requires 10 different >> > places where my images need to load, but I can't have any >> duplicate images >> > show up. > >

« previous php.general (#227755) next »