<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
		>
<channel>
	<title>Comments on: Fast Weighted Random Choice In PHP</title>
	<atom:link href="http://w-shadow.com/blog/2008/12/10/fast-weighted-random-choice-in-php/feed/" rel="self" type="application/rss+xml" />
	<link>http://w-shadow.com/blog/2008/12/10/fast-weighted-random-choice-in-php/</link>
	<description>A blog about web development, software business, and WordPress</description>
	<lastBuildDate>Tue, 22 May 2012 04:00:20 +0000</lastBuildDate>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.2</generator>
	<item>
		<title>By: ilko</title>
		<link>http://w-shadow.com/blog/2008/12/10/fast-weighted-random-choice-in-php/comment-page-1/#comment-198086</link>
		<dc:creator>ilko</dc:creator>
		<pubDate>Sun, 12 Feb 2012 11:55:30 +0000</pubDate>
		<guid isPermaLink="false">http://w-shadow.com/?p=610#comment-198086</guid>
		<description>Also you may want to give the parameters as key-value pairs, not two separate arrays.

Array (
  &#039;id&#039; =&gt; &#039;weight&#039;
}</description>
		<content:encoded><![CDATA[<p>Also you may want to give the parameters as key-value pairs, not two separate arrays.</p>
<p>Array (<br />
  &#8216;id&#8217; =&gt; &#8216;weight&#8217;<br />
}</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: ilko</title>
		<link>http://w-shadow.com/blog/2008/12/10/fast-weighted-random-choice-in-php/comment-page-1/#comment-198085</link>
		<dc:creator>ilko</dc:creator>
		<pubDate>Sun, 12 Feb 2012 11:52:14 +0000</pubDate>
		<guid isPermaLink="false">http://w-shadow.com/?p=610#comment-198085</guid>
		<description>Btw using a function in the for statement will execute the function on every loop so it will be faster if you take count() outside of the loop.

$cweights = count($weights);
for ($i=0; $i&lt;$cweights; $i++) {</description>
		<content:encoded><![CDATA[<p>Btw using a function in the for statement will execute the function on every loop so it will be faster if you take count() outside of the loop.</p>
<p>$cweights = count($weights);<br />
for ($i=0; $i&lt;$cweights; $i++) {</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Jānis Elsts</title>
		<link>http://w-shadow.com/blog/2008/12/10/fast-weighted-random-choice-in-php/comment-page-1/#comment-195236</link>
		<dc:creator>Jānis Elsts</dc:creator>
		<pubDate>Sat, 10 Dec 2011 10:48:29 +0000</pubDate>
		<guid isPermaLink="false">http://w-shadow.com/?p=610#comment-195236</guid>
		<description>You&#039;re right. Edited.</description>
		<content:encoded><![CDATA[<p>You&#8217;re right. Edited.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Corry</title>
		<link>http://w-shadow.com/blog/2008/12/10/fast-weighted-random-choice-in-php/comment-page-1/#comment-195202</link>
		<dc:creator>Corry</dc:creator>
		<pubDate>Fri, 09 Dec 2011 21:09:12 +0000</pubDate>
		<guid isPermaLink="false">http://w-shadow.com/?p=610#comment-195202</guid>
		<description>Am I overlooking something, or is $values not really needed by the calc_lookup() function?

Thanks for the code I think I&#039;ll use it.</description>
		<content:encoded><![CDATA[<p>Am I overlooking something, or is $values not really needed by the calc_lookup() function?</p>
<p>Thanks for the code I think I&#8217;ll use it.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: PHP Random Numbers - DesignersTalk</title>
		<link>http://w-shadow.com/blog/2008/12/10/fast-weighted-random-choice-in-php/comment-page-1/#comment-186396</link>
		<dc:creator>PHP Random Numbers - DesignersTalk</dc:creator>
		<pubDate>Sun, 02 Oct 2011 15:14:32 +0000</pubDate>
		<guid isPermaLink="false">http://w-shadow.com/?p=610#comment-186396</guid>
		<description>[...] for: php random string with weighting - Google Search      Good call, this seems a good solution - Fast Weighted Random Choice In PHP &#124; W-Shadow.com  Cheers for the help!   __________________ Flickr me &#124; My WordPress [...]</description>
		<content:encoded><![CDATA[<p>[...] for: php random string with weighting &#8211; Google Search      Good call, this seems a good solution &#8211; Fast Weighted Random Choice In PHP | W-Shadow.com  Cheers for the help!   __________________ Flickr me | My WordPress [...]</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: macjohn</title>
		<link>http://w-shadow.com/blog/2008/12/10/fast-weighted-random-choice-in-php/comment-page-1/#comment-168147</link>
		<dc:creator>macjohn</dc:creator>
		<pubDate>Mon, 21 Mar 2011 12:38:58 +0000</pubDate>
		<guid isPermaLink="false">http://w-shadow.com/?p=610#comment-168147</guid>
		<description>Nice. But biased to the first element.

function weighted_random_simple($values, $weights){
    $total = array_sum($weights);
    $n = 0; 
    $num = mt_rand(1, $total);
    foreach ($values as $i =&gt; $value) {
        $n += $weights[$i]; 
        if ($n &gt;= $num){
            return $values[$i]; 
        } 
    } 
}

in mt_rand() the $min value must be 1, not zero, since $min and $max are both included. Begining with zero you give the first element the probability of zero plus the probability of the weight.</description>
		<content:encoded><![CDATA[<p>Nice. But biased to the first element.</p>
<p>function weighted_random_simple($values, $weights){<br />
    $total = array_sum($weights);<br />
    $n = 0;<br />
    $num = mt_rand(1, $total);<br />
    foreach ($values as $i =&gt; $value) {<br />
        $n += $weights[$i];<br />
        if ($n &gt;= $num){<br />
            return $values[$i];<br />
        }<br />
    }<br />
}</p>
<p>in mt_rand() the $min value must be 1, not zero, since $min and $max are both included. Begining with zero you give the first element the probability of zero plus the probability of the weight.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Senguttuvan G</title>
		<link>http://w-shadow.com/blog/2008/12/10/fast-weighted-random-choice-in-php/comment-page-1/#comment-117100</link>
		<dc:creator>Senguttuvan G</dc:creator>
		<pubDate>Tue, 02 Nov 2010 11:27:47 +0000</pubDate>
		<guid isPermaLink="false">http://w-shadow.com/?p=610#comment-117100</guid>
		<description>@Nitin: Compiler writers good at making such optimizations as assigning values only once, if the the variable is independent of the loop and many more. We just need to write the code that looks good for us. Even then with your solution, it would be more to write as


for ($i=0, $cweights = count($weights); $i &lt; count($weights); $i++) {

than

$cweights = count($weights);
for ($i=0; $i&lt;count($weights); $i++) {

btw, the article seems to be look. I haven&#039;t read it yet.</description>
		<content:encoded><![CDATA[<p>@Nitin: Compiler writers good at making such optimizations as assigning values only once, if the the variable is independent of the loop and many more. We just need to write the code that looks good for us. Even then with your solution, it would be more to write as</p>
<p>for ($i=0, $cweights = count($weights); $i &lt; count($weights); $i++) {</p>
<p>than</p>
<p>$cweights = count($weights);<br />
for ($i=0; $i&lt;count($weights); $i++) {</p>
<p>btw, the article seems to be look. I haven&#039;t read it yet.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Beanyhead</title>
		<link>http://w-shadow.com/blog/2008/12/10/fast-weighted-random-choice-in-php/comment-page-1/#comment-96446</link>
		<dc:creator>Beanyhead</dc:creator>
		<pubDate>Wed, 29 Sep 2010 18:08:57 +0000</pubDate>
		<guid isPermaLink="false">http://w-shadow.com/?p=610#comment-96446</guid>
		<description>This is an amazing article.  I really didn&#039;t want to write this function myself!  You saved me a lot of work :)</description>
		<content:encoded><![CDATA[<p>This is an amazing article.  I really didn&#8217;t want to write this function myself!  You saved me a lot of work :)</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Nitin</title>
		<link>http://w-shadow.com/blog/2008/12/10/fast-weighted-random-choice-in-php/comment-page-1/#comment-54366</link>
		<dc:creator>Nitin</dc:creator>
		<pubDate>Tue, 15 Jun 2010 14:37:32 +0000</pubDate>
		<guid isPermaLink="false">http://w-shadow.com/?p=610#comment-54366</guid>
		<description>With the calc_lookups function, you can remove the count($weights) from the parameters of the for loop to speed up execution:

$cweights = count($weights);
for ($i=0; $i&lt;count($weights); $i++) {

instead of:

for ($i=0; $i&lt;count($weights); $i++) {

that way the &#039;count&#039; function isn&#039;t called each iteration of the loop.

You could also do small things like remove function paramters from the main function and just force a call to list($lookup, $total_weight) = calc_lookups($values, $weights);, or use incrementation like ++$i; instead of $i++, which executes faster.</description>
		<content:encoded><![CDATA[<p>With the calc_lookups function, you can remove the count($weights) from the parameters of the for loop to speed up execution:</p>
<p>$cweights = count($weights);<br />
for ($i=0; $i&lt;count($weights); $i++) {</p>
<p>instead of:</p>
<p>for ($i=0; $i&lt;count($weights); $i++) {</p>
<p>that way the &#039;count&#039; function isn&#039;t called each iteration of the loop.</p>
<p>You could also do small things like remove function paramters from the main function and just force a call to list($lookup, $total_weight) = calc_lookups($values, $weights);, or use incrementation like ++$i; instead of $i++, which executes faster.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: cecill's</title>
		<link>http://w-shadow.com/blog/2008/12/10/fast-weighted-random-choice-in-php/comment-page-1/#comment-13223</link>
		<dc:creator>cecill's</dc:creator>
		<pubDate>Sat, 13 Dec 2008 23:47:39 +0000</pubDate>
		<guid isPermaLink="false">http://w-shadow.com/?p=610#comment-13223</guid>
		<description>nice tutorial..</description>
		<content:encoded><![CDATA[<p>nice tutorial..</p>
]]></content:encoded>
	</item>
</channel>
</rss>

