Skip to content

Instantly share code, notes, and snippets.

@funkaoshi
Last active December 13, 2015 19:49
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save funkaoshi/4965672 to your computer and use it in GitHub Desktop.
Save funkaoshi/4965672 to your computer and use it in GitHub Desktop.
I was curious what the distribution was when you ask a player to try and roll under a D&D attribute, in general.
import collections
import random
def d(die): return random.randint(1, die)
def xdy(x,y): return sum(d(y) for _ in xrange(x))
# How many times we will peform our simulation
TEST_RUN = 1000000
# Roll up an attribute score 3d6 and proceed to try and roll under it, storing
# the results as a histogram.
results = collections.Counter(xdy(3,6) - d(20) for _ in xrange(TEST_RUN))
sucesses = sum(y for x, y in results.iteritems() if x < 0)
fails = TEST_RUN - sucesses
print "Out of %d rolls, we have %d sucesses and %d fails." % (TEST_RUN, sucesses, fails)
print "--------------"
for x, y in results.iteritems():
if x >= 0:
print "%2d -> %d" % (x, y)
print "--------------"
@funkaoshi
Copy link
Author

A sample run:

Out of 10000000 rolls, we have 5249275 sucesses and 4750725 fails.
--------------
 0 -> 499138
 1 -> 499818
 2 -> 499343
 3 -> 497702
 4 -> 491106
 5 -> 476165
 6 -> 453096
 7 -> 419627
 8 -> 371486
 9 -> 312655
10 -> 250328
11 -> 187186
12 -> 129953
13 ->  81188 
14 ->  46030 
15 ->  22830 
16 ->   9303 
17 ->   2321 
--------------

real  2m24.262s
user  2m23.795s
sys   0m0.223s

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment