class EnvelopeAlloc::Provider
Attributes
alloc_num[R]
item_max[R]
total_num[R]
unit[R]
Public Class Methods
new(total_num, alloc_num, options={})
click to toggle source
# File lib/envelope_alloc.rb, line 8 def initialize total_num, alloc_num, options={} @total_num = total_num @alloc_num = alloc_num @unit = options[:unit] || 100 @item_max = options[:item_max] || (200 * @unit) end
Public Instance Methods
alloc_items()
click to toggle source
# File lib/envelope_alloc.rb, line 15 def alloc_items return [total_num] if alloc_num == 1 money_left = (total_num - alloc_num * unit) value_vec = [] 0.upto(alloc_num-2) do |i| mu = money_left / (alloc_num - i) sigma = mu / 2 rg = RandomGaussian.new(mu, sigma) noise_value = rg.rand.to_i noise_value = 0 if noise_value < 0 noise_value = money_left if noise_value > money_left threshold_value = item_max - unit noise_value = threshold_value if noise_value > threshold_value value_vec << (noise_value + unit) money_left -= noise_value end money_left = (total_num - value_vec.reduce(&:+)) last_money = money_left > item_max ? item_max : money_left value_vec << last_money money_left = money_left - last_money split_left_items(money_left, value_vec) value_vec end
split_left_items(money_left, value_vec)
click to toggle source
# File lib/envelope_alloc.rb, line 42 def split_left_items(money_left, value_vec) return unless money_left > 0 value_vec.map! do |val| gap = item_max - val if money_left > gap money_left -= gap item_max else value = val + money_left money_left = 0 value end end end