Consider a game that uses a generator which produces independent random integers between 1 and 100 inclusive. The game starts with a sum S = 0. The first player adds random numbers from the generator to S until S > 100 and records her last random number 'x'. The second player, continues adding random numbers from the generator to S until S > 200 and records her last random number 'y'. The player with the highest number wins, i.e. if y > x the second player wins. Is this game fair? Write a program to simulate 100,000 games. What is the probability estimate, based on your simulations, that the second player wins? Give your answer rounded to 3 places behind the decimal. For extra credit, calculate the exact probability (without sampling).
import random CONST_TIMES = 100000 CONST_SMALL = 100 CONST_LARGE = 200 def playGame(): s = 0 while s <= CONST_SMALL: x = random.randint(1, CONST_SMALL) s = s + x; while s <= CONST_LARGE: y = random.randint(1, CONST_SMALL) s = s + y if x < y: return 's' elif x == y: return 'm' else: return 'f' fst = sec = 0 for i in range(CONST_TIMES): winner = playGame() if winner == 'f': fst = fst + 1 elif winner == 's': sec = sec + 1 secWinPro = round(float(sec) / CONST_TIMES, 3) print secWinPro
The simulation probability is about 0.524. I want to know how to calculate the exact probability.