10 - choosing randomly from a weighted list.rb

来自「O Reilly Ruby Cookbook source code」· RB 代码 · 共 66 行

RB
66
字号
def choose_weighted(weighted)  sum = weighted.inject(0) do |sum, item_and_weight|     sum += item_and_weight[1]   end  target = rand(sum)  weighted.each do |item, weight|    return item if target <= weight    target -= weight  endend#---marbles = { :black => 51, :white => 17 }3.times { puts choose_weighted(marbles) }# black# white# black#---lottery_probabilities = { "You've wasted your money!" => 1000,  "You've won back the cost of your ticket!" => 50,  "You've won two shiny zorkmids!" => 20,  "You've won five zorkmids!" => 10,  "You've won ten zorkmids!" => 5,  "You've won a hundred zorkmids!" => 1 }# Let's buy some lottery tickets.5.times { puts choose_weighted(lottery_probabilities) }# You've wasted your money!# You've wasted your money!# You've wasted your money!# You've wasted your money!# You've won five zorkmids!#---def normalize!(weighted)  sum = weighted.inject(0) do |sum, item_and_weight|     sum += item_and_weight[1]   end  sum = sum.to_f  weighted.each { |item, weight| weighted[item] = weight/sum }endlottery_probabilities["You've won five hundred zorkmids!"] = 0.1normalize!(lottery_probabilities)# => { "You've wasted your money!" => 0.920725531718995, # =>   "You've won back the cost of your ticket!" => 0.0460362765859497, # =>   "You've won two shiny zorkmids!" => 0.0184145106343799, # =>   "You've won five zorkmids!" => 0.00920725531718995, # =>   "You've won ten zorkmids!" => 0.00460362765859497, # =>   "You've won a hundred zorkmids!" => 0.000920725531718995, # =>   "You've won five hundred zorkmids!" => 9.20725531718995e-05 }#---def choose_weighted_assuming_unity(weighted)  target = rand  weighted.each do |item, weight|    return item if target <= weight    target -= weight  endend5.times { puts choose_weighted_assuming_unity(lottery_probabilities) }# You've wasted your money!# You've wasted your money!# You've wasted your money!# You've wasted your money!# You've won back the cost of your ticket!#---

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?