Last active
October 21, 2020 22:36
-
-
Save mdippery/940934d1a5db3d579de05c44013c3bf6 to your computer and use it in GitHub Desktop.
Calculate expected value of D&D ability generation
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
#!/usr/bin/env python | |
import random | |
import sys | |
def d6(): | |
return random.randrange(1, 7) | |
def fourD6(): | |
return [d6() for _ in range(4)] | |
def topD6(): | |
return sorted(fourD6(), reverse=True)[:3] | |
def sumD6(): | |
return sum(topD6()) | |
ITERATIONS = 1000000 | |
histo = [None, None, None] + [0] * 16 | |
min_, max_, total = 2 ** 32, -1 , 0 | |
for _ in range(ITERATIONS): | |
s = sumD6() | |
print(f"{s}", file=sys.stderr) | |
min_ = min(min_, s) | |
max_ = max(max_, s) | |
total += s | |
histo[s] += 1 | |
ev = total / ITERATIONS | |
print(f"min was {min_}", file=sys.stderr) | |
print(f"max was {max_}", file=sys.stderr) | |
print(f"EV is {ev}") | |
cum = 0.0 | |
for roll, count in enumerate(histo[3:], 3): | |
pc = count / ITERATIONS | |
cum += (100.0 * pc) | |
print(f"{roll:2d} : {pc * 100.0:6.2f}% {cum:6.2f}% ({count})") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment