Sha256: 97db575a6c11441e46332823ebcafcbf92d9874b609495c1da34c382c96cc157

Contents?: true

Size: 410 Bytes

Versions: 24

Compression:

Stored size: 410 Bytes

Contents

# frozen_string_literal: true
module Split
  module Algorithms
    module WeightedSample
      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
    end
  end
end

Version data entries

24 entries across 24 versions & 2 rubygems

Version Path
ab-split-1.0.2 lib/split/algorithms/weighted_sample.rb
ab-split-1.0.1 lib/split/algorithms/weighted_sample.rb
ab-split-1.0.0 lib/split/algorithms/weighted_sample.rb
split-3.4.1 lib/split/algorithms/weighted_sample.rb
split-3.4.0 lib/split/algorithms/weighted_sample.rb
split-3.3.2 lib/split/algorithms/weighted_sample.rb
split-3.3.1 lib/split/algorithms/weighted_sample.rb
split-3.3.0 lib/split/algorithms/weighted_sample.rb
split-3.2.0 lib/split/algorithms/weighted_sample.rb
split-3.1.1 lib/split/algorithms/weighted_sample.rb
split-3.1.0 lib/split/algorithms/weighted_sample.rb
split-3.0.0 lib/split/algorithms/weighted_sample.rb
split-2.2.0 lib/split/algorithms/weighted_sample.rb
split-2.1.0 lib/split/algorithms/weighted_sample.rb
split-2.0.0 lib/split/algorithms/weighted_sample.rb
split-1.7.0 lib/split/algorithms/weighted_sample.rb
split-1.6.0 lib/split/algorithms/weighted_sample.rb
split-1.5.0 lib/split/algorithms/weighted_sample.rb
split-1.4.5 lib/split/algorithms/weighted_sample.rb
split-1.4.4 lib/split/algorithms/weighted_sample.rb