Re: [PHP] Random Images with no duplicates?
Mike [EMAIL PROTECTED] 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 [EMAIL PROTECTED] wrote in message news:[EMAIL PROTECTED] 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()*100); #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. -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] Random Images with no duplicates?
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 [EMAIL PROTECTED] wrote in message news:[EMAIL PROTECTED] 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()*100); #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. -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] Random Images with no duplicates?
Jared Williams wrote: Hi, Just unset the ones you've picked ?php #random images example #this is your file $file = images.txt; #open the file $openFile = file($file); #generate a random number srand((double)microtime()*100); #get one of the entries in the file for ($i = 0; $i 10; ++$i) { $r = array_rand($openFile); $random_image = $openFile[$r]; echo img src='$random_image' height='170' width='100'/img; unset($openFile[$r]); } ? Jared Sorry if I jump in late in this thread, but wouldn't this work? code $file = images.txt; $openFile = file($file); $num_images = 10; # don't need this is PHP 4.2.0 srand((float) microtime() * 1000); $random_keys = array_rand($openFile, $num_images); foreach ($random_keys as $random_key) { echo img src='$random_image' height='170' width='100'/img; } /code array_rand: http://php.net/array_rand Maybe I'm missing something... Anyway, cheers everybody! Silvio -- tradeOver | http://www.tradeover.net ...ready to become the King of the World? -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] Random Images with no duplicates?
Silvio Porcellana [tradeOver] wrote: code $file = images.txt; $openFile = file($file); $num_images = 10; # don't need this is PHP 4.2.0 srand((float) microtime() * 1000); $random_keys = array_rand($openFile, $num_images); foreach ($random_keys as $random_key) { echo img src='$random_image' height='170' width='100'/img; ops, sorry, that obviously should be: echo img src=' . $openFile[$random_key] . ' height='170' width='100'/img; } /code Sorry about this! Silvio -- tradeOver | http://www.tradeover.net ...ready to become the King of the World? -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
RE: [PHP] Random Images with no duplicates?
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. -Original Message- From: Curt Zirzow [mailto:[EMAIL PROTECTED] Sent: Friday, December 16, 2005 2:47 AM To: php-general@lists.php.net Subject: Re: [PHP] Random Images with no duplicates? On Thu, Dec 15, 2005 at 08:45:33PM -0800, Mike Rondeau wrote: Hi, 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()*100); #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. Depending on the complexity of the logic you want in selecting random images you can do something like: $files = file($file); $want = 10;// how many I want $max = count($files); // the range, we might need -1 // a simple range check, to avoid an infinate loop $can_have = $want $max? $max: $want; // start off the loop with these values $have = 0; // aka have none $images = array(); // aka no random images do { // generate a random number $random = mt_rand(0, $max); // if we dont have it.. if(! isset($images[$random]) ) { $have++; $images[$random] = $files[$random]; } // have we made it there yet? } while($have $can_have) var_dump($images); foreach($images as $random_image) { echo img src='$random_image' height='170' width='100'/img; } Now, keep in mind that if you have 15 images and you want 10, this could potentially be very intensive on the cpu. On the other hand if you have 10,000 images and are looking for 10 it wont be as intensive on the cpu, but will use a lot of memory that really shouldn't be used by loading the file into an array. At this point an index file should be used or a DB solution should be considered. Curt. -- cat .signature: No such file or directory -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
RE: [PHP] Random Images with no duplicates?
Hi, Just unset the ones you've picked ?php #random images example #this is your file $file = images.txt; #open the file $openFile = file($file); #generate a random number srand((double)microtime()*100); #get one of the entries in the file for ($i = 0; $i 10; ++$i) { $r = array_rand($openFile); $random_image = $openFile[$r]; echo img src='$random_image' height='170' width='100'/img; unset($openFile[$r]); } ? Jared -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
[PHP] Random Images with no duplicates?
Hi, 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()*100); #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. Any suggestions on what I need to modify here? Thanks in advance! Mike -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
[PHP] Random Images with no duplicates?
Hi, 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()*100); #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. Any suggestions on what I need to modify here? Thanks in advance! Mike -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] Random Images with no duplicates?
This should work, although if there are only 9 lines in the file, it will run infintely (or until the page times out)... so put a check in for that. $imagesNeeded = 10; $imageArr = array(); // Create array with $imagesNeeded number of elements. for($i=0;$i$imagesNeeded;$i++) { // choose image $tmpImg = $openFile[array_rand($openFile)]; // Keep getting images until the image you have is not in the existing array of images while(in_array($tmpImg,$imagesArr)) { // choose image $tmpImg = $openFile[array_rand($openFile)]; } // Put unique image into array array_push($imageArr,$tmpImg); } // You now have an array called imageArr with 10 elements/images in it. Aaron On 12/15/05, Mike Rondeau [EMAIL PROTECTED] wrote: Hi, 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()*100); #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. Any suggestions on what I need to modify here? Thanks in advance! Mike -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] Random Images with no duplicates?
On Thu, Dec 15, 2005 at 08:45:33PM -0800, Mike Rondeau wrote: Hi, 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()*100); #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. Depending on the complexity of the logic you want in selecting random images you can do something like: $files = file($file); $want = 10;// how many I want $max = count($files); // the range, we might need -1 // a simple range check, to avoid an infinate loop $can_have = $want $max? $max: $want; // start off the loop with these values $have = 0; // aka have none $images = array(); // aka no random images do { // generate a random number $random = mt_rand(0, $max); // if we dont have it.. if(! isset($images[$random]) ) { $have++; $images[$random] = $files[$random]; } // have we made it there yet? } while($have $can_have) var_dump($images); foreach($images as $random_image) { echo img src='$random_image' height='170' width='100'/img; } Now, keep in mind that if you have 15 images and you want 10, this could potentially be very intensive on the cpu. On the other hand if you have 10,000 images and are looking for 10 it wont be as intensive on the cpu, but will use a lot of memory that really shouldn't be used by loading the file into an array. At this point an index file should be used or a DB solution should be considered. Curt. -- cat .signature: No such file or directory -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php