Skip to content

Instantly share code, notes, and snippets.

@blinks
Created July 9, 2019 22:23
Show Gist options
  • Save blinks/3cbb0a74273ebcbc93f26572e2267793 to your computer and use it in GitHub Desktop.
Save blinks/3cbb0a74273ebcbc93f26572e2267793 to your computer and use it in GitHub Desktop.
A monte carlo simulation of a very particular kind of dice pool.
#!/usr/bin/env python
# A simple monte carlo simulation for a specific kind of die pool.
#
# Usage: ./pool.py --trials=1000 4d6+3d8
import collections
import random
def main(args):
result = collections.defaultdict(lambda: 0)
for trial in range(args.trials):
highest, count = 0, 0
pool = sorted(roll(args.pattern[0]), reverse=True)
highest = pool[0][0]
for val, die in pool[1:]:
if val == die:
count += 1
result[highest+count] += 1
for val in result:
print '%02d: %0.5f' % (val, result[val] / float(args.trials))
def roll(pattern):
for dice in pattern.split('+'):
n, d = int(dice[0]), int(dice[2])
for _ in range(n):
yield (random.randrange(1, d+1), d)
if __name__ == '__main__':
import argparse
parser = argparse.ArgumentParser()
parser.add_argument('pattern', metavar='P', type=str, nargs=1)
parser.add_argument('--trials', type=int, default=1000)
main(parser.parse_args())
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment