Skip to content

Instantly share code, notes, and snippets.

@ogilviemt
Last active December 26, 2015 09:29
Show Gist options
  • Select an option

  • Save ogilviemt/7130262 to your computer and use it in GitHub Desktop.

Select an option

Save ogilviemt/7130262 to your computer and use it in GitHub Desktop.
Teaching myself Python. This is a simple random dice roller. Input a number of dice to roll (like 3d6) and a modifier if you have one (like bonus damage!) and this snip of code will calculate the total using randint as a randomizer.
from random import randint
while True:
string = str(raw_input("Dice: ")) #how about "3d6", for example
split = string.split("d") #create a list containing 3 and 6
modifier = None
while modifier is None:
modifier = int(raw_input("Modifier: "))
if modifier is "":
modifier = 0
num_dice, hedrons, rolls, total, die = int(split[0]), int(split[1]), 0, 0, 0
while rolls < num_dice: #we will roll the die 3 times
die = randint(1, hedrons) #roll a d6 and discover result
print "1d%d rolled: %d." % (hedrons, die)
rolls = rolls + 1 #roll again if necessary (up to 3 times)
total = total + die #keep a tally of all die rolls
max_damage = num_dice * hedrons + modifier
print "Modifier is %d." % modifier
print "Total roll: %d. (max: %d)" % ((total + modifier), max_damage)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment