Created
March 9, 2012 19:43
-
-
Save bblack/2008289 to your computer and use it in GitHub Desktop.
sim kasper's dice problem
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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