Skip to content

Instantly share code, notes, and snippets.

@bblack
Created March 9, 2012 19:43
Show Gist options
  • Save bblack/2008289 to your computer and use it in GitHub Desktop.
Save bblack/2008289 to your computer and use it in GitHub Desktop.
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