<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DZone Snippets: weight code</title>
    <link>http://snippets.dzone.com/posts</link>
    <pubDate>Thu, 24 Jul 2008 07:53:35 GMT</pubDate>
    <description>DZone Snippets: weight code</description>
    <item>
      <title>Weighted random methods for Array</title>
      <link>http://snippets.dzone.com/posts/show/898</link>
      <description>&lt;code&gt;&lt;br /&gt;class Array&lt;br /&gt;  # Chooses a random array element from the receiver based on the weights&lt;br /&gt;  # provided. If _weights_ is nil, then each element is weighed equally.&lt;br /&gt;  # &lt;br /&gt;  #   [1,2,3].random          #=&gt; 2&lt;br /&gt;  #   [1,2,3].random          #=&gt; 1&lt;br /&gt;  #   [1,2,3].random          #=&gt; 3&lt;br /&gt;  #&lt;br /&gt;  # If _weights_ is an array, then each element of the receiver gets its&lt;br /&gt;  # weight from the corresponding element of _weights_. Notice that it&lt;br /&gt;  # favors the element with the highest weight.&lt;br /&gt;  #&lt;br /&gt;  #   [1,2,3].random([1,4,1]) #=&gt; 2&lt;br /&gt;  #   [1,2,3].random([1,4,1]) #=&gt; 1&lt;br /&gt;  #   [1,2,3].random([1,4,1]) #=&gt; 2&lt;br /&gt;  #   [1,2,3].random([1,4,1]) #=&gt; 2&lt;br /&gt;  #   [1,2,3].random([1,4,1]) #=&gt; 3&lt;br /&gt;  #&lt;br /&gt;  # If _weights_ is a symbol, the weight array is constructed by calling&lt;br /&gt;  # the appropriate method on each array element in turn. Notice that&lt;br /&gt;  # it favors the longer word when using :length.&lt;br /&gt;  #&lt;br /&gt;  #   ['dog', 'cat', 'hippopotamus'].random(:length) #=&gt; "hippopotamus"&lt;br /&gt;  #   ['dog', 'cat', 'hippopotamus'].random(:length) #=&gt; "dog"&lt;br /&gt;  #   ['dog', 'cat', 'hippopotamus'].random(:length) #=&gt; "hippopotamus"&lt;br /&gt;  #   ['dog', 'cat', 'hippopotamus'].random(:length) #=&gt; "hippopotamus"&lt;br /&gt;  #   ['dog', 'cat', 'hippopotamus'].random(:length) #=&gt; "cat"&lt;br /&gt;  def random(weights=nil)&lt;br /&gt;    return random(map {|n| n.send(weights)}) if weights.is_a? Symbol&lt;br /&gt;    &lt;br /&gt;    weights ||= Array.new(length, 1.0)&lt;br /&gt;    total = weights.inject(0.0) {|t,w| t+w}&lt;br /&gt;    point = rand * total&lt;br /&gt;    &lt;br /&gt;    zip(weights).each do |n,w|&lt;br /&gt;      return n if w &gt;= point&lt;br /&gt;      point -= w&lt;br /&gt;    end&lt;br /&gt;  end&lt;br /&gt;  &lt;br /&gt;  # Generates a permutation of the receiver based on _weights_ as in&lt;br /&gt;  # Array#random. Notice that it favors the element with the highest&lt;br /&gt;  # weight.&lt;br /&gt;  #&lt;br /&gt;  #   [1,2,3].randomize           #=&gt; [2,1,3]&lt;br /&gt;  #   [1,2,3].randomize           #=&gt; [1,3,2]&lt;br /&gt;  #   [1,2,3].randomize([1,4,1])  #=&gt; [2,1,3]&lt;br /&gt;  #   [1,2,3].randomize([1,4,1])  #=&gt; [2,3,1]&lt;br /&gt;  #   [1,2,3].randomize([1,4,1])  #=&gt; [1,2,3]&lt;br /&gt;  #   [1,2,3].randomize([1,4,1])  #=&gt; [2,3,1]&lt;br /&gt;  #   [1,2,3].randomize([1,4,1])  #=&gt; [3,2,1]&lt;br /&gt;  #   [1,2,3].randomize([1,4,1])  #=&gt; [2,1,3]&lt;br /&gt;  def randomize(weights=nil)&lt;br /&gt;    return randomize(map {|n| n.send(weights)}) if weights.is_a? Symbol&lt;br /&gt;    &lt;br /&gt;    weights = weights.nil? ? Array.new(length, 1.0) : weights.dup&lt;br /&gt;    &lt;br /&gt;    # pick out elements until there are none left&lt;br /&gt;    list, result = self.dup, []&lt;br /&gt;    until list.empty?&lt;br /&gt;      # pick an element&lt;br /&gt;      result &lt;&lt; list.random(weights)&lt;br /&gt;      # remove the element from the temporary list and its weight&lt;br /&gt;      weights.delete_at(list.index(result.last))&lt;br /&gt;      list.delete result.last&lt;br /&gt;    end&lt;br /&gt;    &lt;br /&gt;    result&lt;br /&gt;  end&lt;br /&gt;end&lt;br /&gt;&lt;/code&gt;&lt;br /&gt;&lt;br /&gt;Intended for use in selecting random elements from an array based on weight. Not optimized!</description>
      <pubDate>Sat, 19 Nov 2005 07:26:41 GMT</pubDate>
      <guid>http://snippets.dzone.com/posts/show/898</guid>
      <author>eventualbuddha (Brian Donovan)</author>
    </item>
  </channel>
</rss>
