Skip to content

Instantly share code, notes, and snippets.

@m-allen-hall
Created July 2, 2023 10:58
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save m-allen-hall/952f8cb374ce109e49dad8922ff381d3 to your computer and use it in GitHub Desktop.
Save m-allen-hall/952f8cb374ce109e49dad8922ff381d3 to your computer and use it in GitHub Desktop.
import random
from statistics import mean
from statistics import median
from statistics import mode
from statistics import stdev
import seaborn as sns
import matplotlib.pyplot as plt
# this function is not really necessary,
# but it makes the gameplay loop easier to read
def roll_die(die_size):
return random.randint(1, die_size)
# this function plays the game and returns the number of rounds played
def play_game():
depth = 2 # pick a starting depth
endGame = 20 # pick the maximum depth
dieSize = 6 # pick the die to use for the game.
rounds = 0 # variable to track the number of rounds played
while True: # this is the gameplay loop
rounds += 1
# tableRoll picks the prompt for this round
tableRoll = roll_die(dieSize) + depth
# check if the game is over
if tableRoll >= endGame:
break
# increment depth only if the D6 roll <= current depth
# this is a good place to tweak how quickly the game advances
# you could also a fixed increment here
depthRoll = roll_die(dieSize)
if depthRoll <= depth:
depth += 1
return rounds
gameData = [] # list to collect the output of the play_game function
numGames = 100000 # how many times do you want to play the game?
# ITERATE
for i in range(numGames):
gameData.append(play_game())
# some basic statistics on the output data
gameData.sort()
print(f'Minimum: {gameData[0]}')
print(f'Maximum: {gameData[(len(gameData)-1)]}')
print(f'Mean: {round(mean(gameData))}')
print(f'Median: {round(median(gameData))}')
print(f'Mode: {round(mode(gameData))}')
print(f'Standard Deviation: {round(stdev(gameData))}')
print(f'90th%ile: {gameData[(int(numGames*0.9))]}')
# basic histogram of the output data
sns.displot(data=gameData)
plt.show()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment