lib/roulette-wheel-selection.rb in roulette-wheel-selection-1.0.0 vs lib/roulette-wheel-selection.rb in roulette-wheel-selection-1.0.1

- old
+ new

@@ -17,10 +17,11 @@ hash = array.map{|v| [v, v[key]] }.to_h return sample_from_hash(hash) end def sample_from_hash(hash) - total_rate = hash.values.inject(&:+) + total_rate = hash.values.inject(&:+) || 0 + return if total_rate == 0 random_seed = rand(total_rate) hash.each do |obj, rate| return obj if random_seed < rate random_seed -= rate end