Skip to content

Instantly share code, notes, and snippets.

Created March 9, 2012 19:43
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
return total
trials = []
(0..999999).each do
trials << get_trial(dice_sides=6, explode_on=5)
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