"Hal E. Fulton" <hal9000 / hypermetrics.com> wrote:

> Given: A set of items, each with
> an associated integer representing
> the relative likelihood that each
> item will be selected.
> 
> Write: A method that will select
> a random item.

def random_weighted(items)
  total = 0
  pick = nil
  items.each do |item, weight|
    pick = item if rand(total += weight) < weight
  end
  pick
end

-- 
Tabby