Module: ProbRand

Defined in:
lib/prob_rand.rb,
lib/prob_rand/version.rb

Defined Under Namespace

Classes: Error

Constant Summary collapse

VERSION =
"0.1.0"

Class Method Summary collapse

Class Method Details

.random_num(numbers, quantities) ⇒ Object



6
7
8
9
10
11
12
13
14
15
16
# File 'lib/prob_rand.rb', line 6

def self.random_num(numbers, quantities)
  total = quantities.sum.to_f
  probabilities = quantities.collect { |input| (input / total.to_f).round(2) }
  randomnumber = rand(0.01..1.0).round(2)
  choose = 0
  probabilities.each_with_index do |value, index|
    choose += value
    return numbers[index] if choose >= randomnumber
  end
  nil
end