Created
February 3, 2010 01:21
-
-
Save mboeh/293242 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
# Messing with some experimental statistical analysis for infinitely exploding dice | |
def roll_die(d) | |
rand(d) + 1 | |
end | |
def roll_exploding_die(d) | |
r = roll_die(d) | |
if r == d | |
r + roll_exploding_die(d) | |
else | |
r | |
end | |
end | |
def roll_exploding_dice(n, d) | |
(1 .. n).map do | |
roll_exploding_die(d) | |
end | |
end | |
def roll_success(d, s) | |
r = roll_die(d) | |
if r == d | |
1 + roll_success(d, s) | |
elsif r >= s | |
1 | |
else | |
0 | |
end | |
end | |
def roll_successes(n, d, s = d - 2) | |
sum = 0 | |
n.times do | |
sum += roll_success(d, s) | |
end | |
sum | |
end | |
5_000_000.times do | |
p roll_successes(6, 6) | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment