# Pitching a Complete Game

Python Puzzles

Back to the Python! homepage

Your All-Star baseball pitcher has a 15% chance of pitching a complete game. After how many starts does the pitcher have a 90% chance of pitching a complete game?

The pitcher has a 90% chance of pitching a complete game after 15 games. The average number of games to pitch a complete game is 6.7 games.

Here is the Python code to run the simulation.

import random
import numpy as np

# Set up the simulation parameters
num_sims = 10000  # number of simulations to run
comp_game_prob = 0.15  # probability of a complete game

# Simulate the games and record the number of games until a complete game
num_games_to_comp_game = []
for i in range(num_sims):
num_games = 0
while True:
num_games += 1
if random.random() <= comp_game_prob:
num_games_to_comp_game.append(num_games)
break

# Calculate the 90th percentile
percentile_90 = np.percentile(num_games_to_comp_game, 90)

# Print the results
print(f"Average number of games to a complete game: {sum(num_games_to_comp_game) / len(num_games_to_comp_game)}")
print(f"Number of games at the 90th percentile: {percentile_90}")