Sha256: 89fd48b5f823c9aa6df8b781f706826bfe7b23d35219c39c8b2831fbe0d2817c
Contents?: true
Size: 658 Bytes
Versions: 9
Compression:
Stored size: 658 Bytes
Contents
class Array def weighted_sample(weights=nil) weights = Array.new(length, 1.0) if weights.nil? || weights.sum == 0 total = weights.sum # The total sum of weights is multiplied by a random number trigger = Kernel::rand * total subtotal = 0 result = nil # The subtotal is checked agains the trigger. The higher the sum, the higher # the probability of triggering a result. weights.each_with_index do |weight, index| subtotal += weight if subtotal > trigger result = self[index] break end end # Returns self.last from current array if result is nil result || last end end
Version data entries
9 entries across 9 versions & 1 rubygems