Last active
October 20, 2023 14:15
-
-
Save tngzng/1282b92a288681e8309c943cbb156a4e to your computer and use it in GitHub Desktop.
Tennis Scoring: Relationship b/w Points Won + Games Won
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Game: | |
def __init__(self): | |
self.p1_points = 0 | |
self.p2_points = 0 | |
def who_won(self): | |
''' | |
0, 1, 2, 3, 4 | |
0, 15, 30, 40, win (need 4 points to win, and to win by 2) | |
''' | |
if self.p1_points >= 4 and self.p1_points - self.p2_points >= 2: | |
return 'p1' | |
if self.p2_points >= 4 and self.p2_points - self.p1_points >= 2: | |
return 'p2' | |
import random | |
P1_CHANCES = range(50, 100, 1) | |
def p1_wins(percent): | |
return random.randrange(100) < percent | |
from collections import Counter | |
ROUNDS = 10_000 | |
p1_results = [] | |
for p1_chance in P1_CHANCES: | |
wins = Counter() | |
for _ in range(ROUNDS): | |
game = Game() | |
while not game.who_won(): | |
if p1_wins(p1_chance): | |
game.p1_points += 1 | |
else: | |
game.p2_points += 1 | |
wins[game.who_won()] += 1 | |
p1_result = (wins["p1"]/ROUNDS) * 100 | |
p1_results.append(p1_result) | |
print(f'when p1 has a {p1_chance}% chance to win each point, p1 wins {p1_result}% of games\n') | |
relationships = [rel for rel in zip(P1_CHANCES, p1_results)] | |
print(f'relationships:\n {relationships}') |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
output from local run shows a non-linear relationship b/w the odds of winning a point and the odds of winning a game, with the odds of winning a game increasing faster as the odds of winning a point increase until an inflection point when the chance to win each point is ~70%: