Module: WeightedLottery

Defined in:
lib/weighted_lottery.rb,
lib/weighted_lottery/version.rb

Constant Summary collapse

VERSION =
"0.1.0"

Class Method Summary collapse

Class Method Details

.pick(items) ⇒ Object



5
6
7
8
9
10
11
12
13
14
15
16
17
# File 'lib/weighted_lottery.rb', line 5

def pick items
  total = items.values.sum
  random = SecureRandom.random_number(total)
  chosen_type = nil
  items.each do |k, v|
    if random <= v
      chosen_type = k
      break
    end
    random -= v
  end
  chosen_type
end