Skip to content

Instantly share code, notes, and snippets.

@Streamweaver
Created January 2, 2021 05:50
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 Streamweaver/28d5e896c039d6dea42868648661dbc2 to your computer and use it in GitHub Desktop.
Save Streamweaver/28d5e896c039d6dea42868648661dbc2 to your computer and use it in GitHub Desktop.
Quick simulator of DnD Rolls in different conditions
import random
N = 100000 # Set this to the number of simulated rolls to aggregate
def d20():
return random.randint(1, 21)
def count_20s(lst):
count = 0
for ele in lst:
if ele == 20:
count = count + 1
return count
def avg_roll(rolls):
return round(sum(rolls) / len(rolls))
def ratio_crits(rolls):
return round((count_20s(rolls) / N) * 100)
def average_triple_advantage(n=N):
rolls = []
for _ in range(n):
ea_roll = [max(d20(), d20()), d20()]
rolls.append(max(ea_roll))
return [avg_roll(rolls), ratio_crits(rolls)]
def average_advantage(n=N):
rolls = [max(d20(), d20()) for x in range(n)]
return [avg_roll(rolls), ratio_crits(rolls)]
def average_of_d20(n=N):
rolls = [d20() for x in range(n)]
return [avg_roll(rolls), ratio_crits(rolls)]
if __name__ == "__main__":
print("Avg roll on 1d20 is {0} and {1}% are crits.".format(*average_of_d20()))
print("Avg roll with Adv is {0} and {1}% are crits.".format(*average_advantage()))
print("Avg roll with EA is {0} and {1}% are crits.".format(*average_triple_advantage()))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment