<?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: Weighted random generation in Python</title>
	<atom:link href="http://eli.thegreenplace.net/2010/01/22/weighted-random-generation-in-python/feed/" rel="self" type="application/rss+xml" />
	<link>http://eli.thegreenplace.net/2010/01/22/weighted-random-generation-in-python/</link>
	<description>Eli Bendersky's personal website</description>
	<lastBuildDate>Wed, 28 Jul 2010 12:22:47 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.9.2</generator>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
		<item>
		<title>By: pronostic</title>
		<link>http://eli.thegreenplace.net/2010/01/22/weighted-random-generation-in-python/comment-page-1/#comment-299899</link>
		<dc:creator>pronostic</dc:creator>
		<pubDate>Tue, 27 Jul 2010 15:02:13 +0000</pubDate>
		<guid isPermaLink="false">http://eli.thegreenplace.net/?p=2051#comment-299899</guid>
		<description>Thanks for sharing this &quot;Walker&#039;s alias method&quot;. Very interesting !</description>
		<content:encoded><![CDATA[<p>Thanks for sharing this &#8220;Walker&#8217;s alias method&#8221;. Very interesting !</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: denis</title>
		<link>http://eli.thegreenplace.net/2010/01/22/weighted-random-generation-in-python/comment-page-1/#comment-288122</link>
		<dc:creator>denis</dc:creator>
		<pubDate>Mon, 14 Jun 2010 14:33:37 +0000</pubDate>
		<guid isPermaLink="false">http://eli.thegreenplace.net/?p=2051#comment-288122</guid>
		<description>hi Eli,
  you might look at Walker&#039;s alias method, in Knuth Graphbase p. 392 or
http://code.activestate.com/recipes/576564-walkers-alias-method-for-random-objects-with-diffe/

and keep up the good posts.
(Care to start a Real Doc project, collecting best sw doc in various categories ?)

cheers
  -- denis</description>
		<content:encoded><![CDATA[<p>hi Eli,<br />
  you might look at Walker&#8217;s alias method, in Knuth Graphbase p. 392 or<br />
<a href="http://code.activestate.com/recipes/576564-walkers-alias-method-for-random-objects-with-diffe/" rel="nofollow">http://code.activestate.com/recipes/576564-walkers-alias-method-for-random-objects-with-diffe/</a></p>
<p>and keep up the good posts.<br />
(Care to start a Real Doc project, collecting best sw doc in various categories ?)</p>
<p>cheers<br />
  &#8212; denis</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: eliben</title>
		<link>http://eli.thegreenplace.net/2010/01/22/weighted-random-generation-in-python/comment-page-1/#comment-254006</link>
		<dc:creator>eliben</dc:creator>
		<pubDate>Sat, 23 Jan 2010 07:48:19 +0000</pubDate>
		<guid isPermaLink="false">http://eli.thegreenplace.net/?p=2051#comment-254006</guid>
		<description>@Evan, interesting, thanks. The numpy version is a bit faster than the bisection method for very long lists, but for short lists it isn&#039;t very good. The method Simon presents is faster in all cases

@Simon, nice variation! It&#039;s really fast - I think I&#039;ll even add it to the article.</description>
		<content:encoded><![CDATA[<p>@Evan, interesting, thanks. The numpy version is a bit faster than the bisection method for very long lists, but for short lists it isn&#8217;t very good. The method Simon presents is faster in all cases</p>
<p>@Simon, nice variation! It&#8217;s really fast &#8211; I think I&#8217;ll even add it to the article.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Simon Davy</title>
		<link>http://eli.thegreenplace.net/2010/01/22/weighted-random-generation-in-python/comment-page-1/#comment-253844</link>
		<dc:creator>Simon Davy</dc:creator>
		<pubDate>Fri, 22 Jan 2010 23:47:11 +0000</pubDate>
		<guid isPermaLink="false">http://eli.thegreenplace.net/?p=2051#comment-253844</guid>
		<description>Similar to a &quot;roulette&quot; select, it&#039;s used a lot in genetic algorithms[1].

In the first version, I think this would be better as it needs no extra list. 

`&lt;code&gt;choice = random() * sum(weights)
for i, w in enumerate(weights):
    choice -= w
    if choice __abENT__lt; 0:
        return i&lt;/code&gt;`

If you can contrive to order the weights in descending order, then this would definitely be quicker I think. 

If you can prebuild the totals list, then the bisect is probably faster. 

Of course, if you can guarantee the weights are ints you can build a simple list and use a single random int look up.

I think this may be covered in TAOCP - but then, what isn&#039;t? :)

[1] http://en.wikipedia.org/wiki/Fitness_proportionate_selection</description>
		<content:encoded><![CDATA[<p>Similar to a &#8220;roulette&#8221; select, it&#8217;s used a lot in genetic algorithms[1].</p>
<p>In the first version, I think this would be better as it needs no extra list. </p>
<div class="backtick"><pre><code>choice = random() * sum(weights)
for i, w in enumerate(weights):
    choice -= w
    if choice &lt; 0:
        return i</code></pre></div>
<p>If you can contrive to order the weights in descending order, then this would definitely be quicker I think. </p>
<p>If you can prebuild the totals list, then the bisect is probably faster. </p>
<p>Of course, if you can guarantee the weights are ints you can build a simple list and use a single random int look up.</p>
<p>I think this may be covered in TAOCP &#8211; but then, what isn&#8217;t? <img src='http://eli.thegreenplace.net/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> </p>
<p>[1] <a href="http://en.wikipedia.org/wiki/Fitness_proportionate_selection" rel="nofollow">http://en.wikipedia.org/wiki/Fitness_proportionate_selection</a></p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Richard Tew</title>
		<link>http://eli.thegreenplace.net/2010/01/22/weighted-random-generation-in-python/comment-page-1/#comment-253751</link>
		<dc:creator>Richard Tew</dc:creator>
		<pubDate>Fri, 22 Jan 2010 18:57:35 +0000</pubDate>
		<guid isPermaLink="false">http://eli.thegreenplace.net/?p=2051#comment-253751</guid>
		<description>This should be in the &#039;random&#039; standard library module.  It&#039;s just one of those things that you end up writing in the course of the development of a code base.  I used it all over the place in EVE Online, from NPC spawning to loot generation.</description>
		<content:encoded><![CDATA[<p>This should be in the &#8216;random&#8217; standard library module.  It&#8217;s just one of those things that you end up writing in the course of the development of a code base.  I used it all over the place in EVE Online, from NPC spawning to loot generation.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Evan Friis</title>
		<link>http://eli.thegreenplace.net/2010/01/22/weighted-random-generation-in-python/comment-page-1/#comment-253694</link>
		<dc:creator>Evan Friis</dc:creator>
		<pubDate>Fri, 22 Jan 2010 17:18:14 +0000</pubDate>
		<guid isPermaLink="false">http://eli.thegreenplace.net/?p=2051#comment-253694</guid>
		<description>There is also a pretty handy way to do this with numpy:

`&lt;code&gt;import numpy as np

def weighted_choice(weights):
    totals = np__abENT__#46;cumsum(weights)
    norm = totals[-1] 
    throw = np__abENT__#46;random__abENT__#46;rand()*norm
    return np__abENT__#46;searchsorted(totals, throw)&lt;/code&gt;`</description>
		<content:encoded><![CDATA[<p>There is also a pretty handy way to do this with numpy:</p>
<div class="backtick"><pre><code>import numpy as np

def weighted_choice(weights):
    totals = np&#46;cumsum(weights)
    norm = totals[-1]
    throw = np&#46;random&#46;rand()*norm
    return np&#46;searchsorted(totals, throw)</code></pre></div>
]]></content:encoded>
	</item>
</channel>
</rss>
