#!/usr/bin/env python import math import random import getopt import sys import re num_results = 1 num_rolls = 1 num_sides = 20 bonus = 0 def generate_roll(): total = 0 for i in range(0, num_rolls): total = total + math.ceil(random.random()*num_sides) total = total + bonus return total def parse_input(): global num_results global num_rolls global num_sides global bonus #grab input (optlist, args) = getopt.getopt(sys.argv[1:], 'n:') #grab optional input of number of results if (len(optlist) > 0): (something, num_results) = optlist[0] num_results = int(num_results) else: num_results = 1 #validate rollstring rollstring = "" for i in range(0, len(args)): rollstring += args[i] #check for valid roll string rollstringregexp = re.compile("^[0-9]+d[0-9]+([+-][0-9]+)?$") if(rollstringregexp.match(rollstring) == None): print "invalid roll string" exit() #ensure valid values rollstring = rollstring.replace("d"," ").replace("+", " ").replace("-", " ") diceroll = rollstring.split(" ") if(len(diceroll) == 2): diceroll.append("0") #set values (num_rolls, num_sides, bonus) = diceroll num_rolls = int(num_rolls) num_sides = int(num_sides) bonus = int(bonus) parse_input() if(bonus > 0): print "Dice Roll:", str(num_rolls) + "d" + str(num_sides) + "+" + str(bonus) else: print "Dice Roll:", str(num_rolls) + "d" + str(num_sides) for i in range(0, num_results): print int(generate_roll())