> You're talking about some kind of proportional selection scheme (hey, you 
> wouldn't happen to be working on Genetic Algorithms, would you?).  One way 
> would be to do what's called roulette wheel selection.  You can visualize 
> a roulette wheel where each number is sized proportionally to the weight.
> The bigger the weight, the more likely the 'roulette wheel' is to land on 
> that item.

Wonderful for visualization!

Make spin() use binary search and it will be faster (mostly useful when
spin() is called multiple times on the same list of weights, of course).

--

Kero

> class RouletteWheel
>   def initialize(itemList)
>     @rouletteList = []
>     runningTotal = 0    
>     @items = itemList
>     calc_sum
>     @items.each_with_index{ |w,i|
>       prob = calc_prob(w)
>       runningTotal += prob
>       @rouletteList[i] = runningTotal
>     }
>   end
> 
>   def spin
>     rn = rand
>     prev = 0.0
>     @items.each_with_index { |w,i|
>       @rouletteList.each_with_index { |p,j|
>         if rn.between?(prev,p)
>           return @items[j]
>         end
>         prev = p
>       }
>     }
>     #otherwise return the last one:
>     return @items[-1]
>   end
> 
>   private
>   def calc_sum
      @sum = 0
>     @items.each { |w|
>       @sum += w
>     }
>   end
> 
>   def calc_prob(value)
>     value/@sum.to_f
>   end
> 
> end