Sha256: 9a5d6cef02c154ddc5b27facf53a0b82da627c794eac1326d53bae0f9ca1a331

Contents?: true

Size: 412 Bytes

Versions: 6

Compression:

Stored size: 412 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

6 entries across 6 versions & 1 rubygems

Version Path
split-4.0.4 lib/split/algorithms/weighted_sample.rb
split-4.0.3 lib/split/algorithms/weighted_sample.rb
split-4.0.2 lib/split/algorithms/weighted_sample.rb
split-4.0.1 lib/split/algorithms/weighted_sample.rb
split-4.0.0.pre2 lib/split/algorithms/weighted_sample.rb
split-4.0.0.pre lib/split/algorithms/weighted_sample.rb