ada_heat_is_on.py

This python3 program was used to calculate the probalities as presented in RPG Design - The Heat is On!. It is neither awfully pretty or efficient, but it got the job done.

#-*- coding: utf-8 -*-

from functools import partial
import random

SIM = 10**6

def reroll(dice):
    for _ in range(2):
        result = max([random.randint(1, 6) for _ in range(dice)])
        if result > 3:
            return 1
    return 0

def bonus(dice, n):
    result = max([random.randint(1, 6) for _ in range(dice + n)])
    if result > 3:
        return 1
    return 0

def simulate(func):
    result = 0
    for _ in range(SIM):
        result += func()
    return float(result) / float(SIM)

def main():
    import sys

    if len(sys.argv) < 2:
        print("usage: {} <base>".format(sys.argv[0]), file=sys.stderr)
        sys.exit(-1)

    base = int(sys.argv[1], base=10)
    print("reroll: {}".format(simulate(partial(reroll, base))))
    print("+1: {}".format(simulate(partial(bonus, base, 1))))
    print("+2: {}".format(simulate(partial(bonus, base, 2))))
    print("+3: {}".format(simulate(partial(bonus, base, 3))))
    print("+4: {}".format(simulate(partial(bonus, base, 4))))

if __name__ == "__main__":
    main()