Skip to content

Instantly share code, notes, and snippets.

@bblack
Created Mar 9, 2012
Embed
What would you like to do?
sim kasper's dice problem
def get_trial(dice_sides=6, explode_on=6)
total = 0
while true
roll = rand(dice_sides) + 1 # rand(x) is int in 0..(x-1)
total += roll
break if roll < explode_on
end
return total
end
trials = []
(0..999999).each do
trials << get_trial(dice_sides=6, explode_on=5)
end
avg = trials.inject { |sum, trial| sum + trial }.to_f / trials.count
puts "Average over #{trials.count} trials was #{avg}"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment