samedi 11 mai 2019

How to calculate the exact probability that the second player wins?

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

def playGame():
    s = 0
    while s < 200:
        while s < 100: 
            x = random.randint(1, 100)
            s = s + x;
        y = random.randint(1, 100)
        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 result is about 0.516. I want to know how to calculate the exact probability that the second player wins.




Aucun commentaire:

Enregistrer un commentaire