Re: [PHP] multiple random items
On Mon, July 16, 2007 9:40 am, [EMAIL PROTECTED] wrote: > Hi I have this script which pulls 1 random item from a txt file. > How would I modify it to pull 10 random items. To forestall a rather lengthy discussion on what you mean by 10 random items, I'm actually not going to answer that question. I'm going to answer "10 unique items selected at random" instead. :-) [I.e., I assume you don't want the same random quote to appear twice in one list.] > $delim = "\n"; > $quotefile = "names.txt"; > $fp = fopen($quotefile, "r"); > $contents = fread($fp, filesize($quotefile)); > $quote_arr = explode($delim,$contents); > fclose($fp); > // generate random quote index $uniques = array(); while (count($uniques) < 10){ > $quote_index = (mt_rand(1, sizeof($quote_arr)) - 1); if (isset($uniques[$quote_index])) continue; $uniques[$quote_index] = $quote_index; > // get quote at $quote_index and return it > $herequote = $quote_arr[$quote_index]; > echo "$herequote"; } > ?> YMMV -- Some people have a "gift" link here. Know what I want? I want you to buy a CD from some indie artist. http://cdbaby.com/browse/from/lynch Yeah, I get a buck. So? -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
RE: [PHP] multiple random items
> -Original Message- > From: [EMAIL PROTECTED] [mailto:[EMAIL PROTECTED] > Sent: 16 July 2007 15:40 > To: php-general@lists.php.net > Subject: [PHP] multiple random items > > > Hi I have this script which pulls 1 random item from a txt file. > How would I modify it to pull 10 random items. > > $delim = "\n"; > $quotefile = "names.txt"; > $fp = fopen($quotefile, "r"); > $contents = fread($fp, filesize($quotefile)); > $quote_arr = explode($delim,$contents); > fclose($fp); $random_keys = array_rand($quote_arr, min(count($quote_arr), 10)); foreach ($random_keys as $quote_index) { // get quote at $quote_index and return it $herequote = $quote_arr[$quote_index]; echo "$herequote"; } ?> This won't produce any duplicates and will cope with any number of items. Edward -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] multiple random items
2007. 07. 16, hétfő keltezéssel 10.56-kor Robert Cummings ezt írta: > On Mon, 2007-07-16 at 16:49 +0200, Zoltán Németh wrote: > > 2007. 07. 16, hétfő keltezéssel 15.40-kor [EMAIL PROTECTED] ezt írta: > > > Hi I have this script which pulls 1 random item from a txt file. > > > How would I modify it to pull 10 random items. > > > > > > > > $delim = "\n"; > > > $quotefile = "names.txt"; > > > $fp = fopen($quotefile, "r"); > > > $contents = fread($fp, filesize($quotefile)); > > > $quote_arr = explode($delim,$contents); > > > fclose($fp); > > > > $quotenum = 0; > > $quotes_displayed = array(); > > while ($quotenum < 10) { > > while (!in_array($quote_index, $quotes_displayed)) { > > I think you should remove the ! operator :) oh yeah, typo sorry greets Zoltán Németh > > Cheers, > Rob. -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] multiple random items
On Mon, 2007-07-16 at 16:49 +0200, Zoltán Németh wrote: > 2007. 07. 16, hétfő keltezéssel 15.40-kor [EMAIL PROTECTED] ezt írta: > > Hi I have this script which pulls 1 random item from a txt file. > > How would I modify it to pull 10 random items. > > > > > $delim = "\n"; > > $quotefile = "names.txt"; > > $fp = fopen($quotefile, "r"); > > $contents = fread($fp, filesize($quotefile)); > > $quote_arr = explode($delim,$contents); > > fclose($fp); > > $quotenum = 0; > $quotes_displayed = array(); > while ($quotenum < 10) { > while (!in_array($quote_index, $quotes_displayed)) { I think you should remove the ! operator :) Cheers, Rob. -- ... SwarmBuy.com - http://www.swarmbuy.com Leveraging the buying power of the masses! ... -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] multiple random items
On Mon, 2007-07-16 at 16:49 +0200, Zoltán Németh wrote: > 2007. 07. 16, hétfő keltezéssel 15.40-kor [EMAIL PROTECTED] ezt írta: > > Hi I have this script which pulls 1 random item from a txt file. > > How would I modify it to pull 10 random items. > > > > > $delim = "\n"; > > $quotefile = "names.txt"; > > $fp = fopen($quotefile, "r"); > > $contents = fread($fp, filesize($quotefile)); > > $quote_arr = explode($delim,$contents); > > fclose($fp); > > $quotenum = 0; > $quotes_displayed = array(); > while ($quotenum < 10) { > while (!in_array($quote_index, $quotes_displayed)) { Oh sure!!! Go for unique random items ;) But what happens if the file contains less than 10 items :) Cheers, Rob. -- ... SwarmBuy.com - http://www.swarmbuy.com Leveraging the buying power of the masses! ... -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] multiple random items
On Mon, 2007-07-16 at 15:40 +0100, [EMAIL PROTECTED] wrote: > Hi I have this script which pulls 1 random item from a txt file. > How would I modify it to pull 10 random items. > > $delim = "\n"; > $quotefile = "names.txt"; > $fp = fopen($quotefile, "r"); > $contents = fread($fp, filesize($quotefile)); > $quote_arr = explode($delim,$contents); > fclose($fp); $herequotes = array(); for( $i = 0; $i < 10; $i++ ) { > // generate random quote index > $quote_index = (mt_rand(1, sizeof($quote_arr)) - 1); > // get quote at $quote_index and return it > $herequote = $quote_arr[$quote_index]; $herequotes[] = $herequote; } > echo "$herequote"; > ?> > > Thanks You're welcome. Cheers, Rob. -- ... SwarmBuy.com - http://www.swarmbuy.com Leveraging the buying power of the masses! ... -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] multiple random items
2007. 07. 16, hétfő keltezéssel 15.40-kor [EMAIL PROTECTED] ezt írta: > Hi I have this script which pulls 1 random item from a txt file. > How would I modify it to pull 10 random items. > > $delim = "\n"; > $quotefile = "names.txt"; > $fp = fopen($quotefile, "r"); > $contents = fread($fp, filesize($quotefile)); > $quote_arr = explode($delim,$contents); > fclose($fp); $quotenum = 0; $quotes_displayed = array(); while ($quotenum < 10) { while (!in_array($quote_index, $quotes_displayed)) { > // generate random quote index > $quote_index = (mt_rand(1, sizeof($quote_arr)) - 1); } > // get quote at $quote_index and return it > $herequote = $quote_arr[$quote_index]; > echo "$herequote"; $quotes_displayed[] = $quote_index; $quotenum++; } > ?> > hope that helps greets Zoltán Németh > Thanks > -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php