Sha256: 7309b55e79f40e87c0f54bc252d81f509ca26cf3e9b6017a708439764e08e4d1

Contents?: true

Size: 310 Bytes

Versions: 1

Compression:

Stored size: 310 Bytes

Contents

class Array
  def get_rand(weights)
    raise "Wrong number of array elements!" unless weights.size == self.size

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

    self.each_with_index do |element, i|
      return element if weights[i] >= point
      point -= weights[i]
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
guided_randomness-0.1.3 lib/guided_randomness.rb