module MoreMarkov
Constants
- VERSION
Public Class Methods
weighted_choice(hash)
click to toggle source
# File lib/more_markov.rb, line 4 def self.weighted_choice(hash) total = hash.inject(0) { |sum, item_and_weight| sum += item_and_weight[1] } target = rand(total) hash.each do |item, weight| return item if target <= weight target -= weight end end