Skip to content

Instantly share code, notes, and snippets.

@trbarron
Created October 1, 2019 21:30
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 trbarron/a7dff138f2f9994d38c62d42ab93bc93 to your computer and use it in GitHub Desktop.
Save trbarron/a7dff138f2f9994d38c62d42ab93bc93 to your computer and use it in GitHub Desktop.
import random
max_outs = 3
innings = 9
game_num = 100000 #num games to run for each matchup
def batter_up(batting_power,batting_perc,i_bases,i_outs):
hit = random.random() < batting_perc
if hit:
if batting_power == 1:
#if you hit a single, advance runners and put a runner on first
i_bases[3] += i_bases [2]
i_bases[2] = i_bases[1]
i_bases[1] = i_bases[0]
i_bases[0] = 1
if batting_power == 2:
#if you hit a double, advance runners two and put a runner on second
i_bases[3] += i_bases [1]
i_bases[1] = 1
if batting_power == 4:
#if you hit a double, advance runners two and put a runner on second
i_bases[3] += 1
else: i_outs +=1
return i_bases, i_outs
def play_inning(batting_power,batting_perc):
i_outs = 0
i_runs = 0
i_bases = [0,0,0,0]
while i_outs < max_outs:
i_bases, i_outs = batter_up(batting_power,batting_perc,i_bases,i_outs)
i_runs = i_bases[3]
return i_runs
def extra_innings(t1_bat_power,t1_bat_perc,t2_bat_power,t2_bat_perc):
scores = [0,0]
#play innings until a team is in the lead
while scores[0] == scores[1]:
scores[0] += play_inning(t1_bat_power,t1_bat_perc)
scores[1] += play_inning(t2_bat_power,t2_bat_perc)
return scores[0] > scores[1]
def play_game(t1_bat_power,t1_bat_perc,t2_bat_power,t2_bat_perc):
scores = [0,0]
#play innings
for i in range(innings):
scores[0] += play_inning(t1_bat_power,t1_bat_perc)
scores[1] += play_inning(t2_bat_power,t2_bat_perc)
#score is a tie
if scores[0] == scores[1]:
return extra_innings(t1_bat_power,t1_bat_perc,t2_bat_power,t2_bat_perc)
#otherwise return if team 1 is bigger than 2
else: return scores[0] > scores[1]
#Run games (can be done more efficiently but w/e)
#Singles team:
t1_wins = 0
for i in range(game_num):
t1_wins += play_game(1,0.4,2,0.2)
t1_wins += play_game(1,0.4,4,0.1)
print("Single team: ",float(t1_wins))
#Doubles team:
t2_wins = 0
for i in range(game_num):
t2_wins += play_game(2,0.2,1,0.4)
t2_wins += play_game(2,0.2,4,0.1)
print("Doubles team: ",float(t2_wins))
#Homers team:
t3_wins = 0
for i in range(game_num):
t3_wins += play_game(4,0.1,2,0.2)
t3_wins += play_game(4,0.1,1,0.4)
print("Homers team: ",float(t3_wins))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment