Module: Split::Algorithms::WeightedSample

Defined in:
lib/split/algorithms/weighted_sample.rb

Class Method Summary collapse

Class Method Details

.choose_alternative(experiment) ⇒ Object


4
5
6
7
8
9
10
11
12
13
14
# File 'lib/split/algorithms/weighted_sample.rb', line 4

def self.choose_alternative(experiment)
  weights = experiment.alternatives.map(&:weight)

  total = weights.inject(:+)
  point = rand * total

  experiment.alternatives.zip(weights).each do |n,w|
    return n if w >= point
    point -= w
  end
end