Module: MoreMarkov

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

Defined Under Namespace

Classes: MarkovChainGenerator

Constant Summary collapse

VERSION =
"0.1.0"

Class Method Summary collapse

Class Method Details

.weighted_choice(hash) ⇒ Object



4
5
6
7
8
9
10
11
# 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