Sha256: 37a7d94c8e6c816bc32b3ca4fa111db4106d0c8b1c7a56ed1b9234bcfbbcb1ff
Contents?: true
Size: 754 Bytes
Versions: 6
Compression:
Stored size: 754 Bytes
Contents
# frozen_string_literal: true class WeightedList class Sampler def initialize(hash:, random:) @hash = hash @random = random end def call Result.new(selected, remaining) end private attr_reader :hash, :random Result = Struct.new(:selected, :remaining) def select return if hash.empty? current_target = random.rand(total_weight) hash.each do |item, weight| return item if current_target <= weight current_target -= weight end end def selected @selected ||= select end def remaining hash.reject { |item, _weight| selected == item } end def total_weight @total_weight ||= hash.values.reduce(&:+) end end end
Version data entries
6 entries across 6 versions & 1 rubygems