simulateur du jeu de Mounty Hall

This commit is contained in:
nojhan 2012-03-18 22:12:09 +01:00
commit b6e21095bb
2 changed files with 53 additions and 0 deletions

1
README
View file

@ -9,6 +9,7 @@ Jeux de base
* Jeu du pendu
* Mastermind
* Ordonner par permutations
* Simulateur de Mounty Hall
Jeux de tableaux
----------------

52
src/mounty_hall.py Normal file
View file

@ -0,0 +1,52 @@
#encoding: utf-8
import random
def play( swap, verbose = True ):
""" Joue une partie du jeu de Mounty Hall et renvoie vrai si la partie est gagnée """
wins = 0
doors = ["chèvre","chèvre","voiture"]
random.shuffle(doors)
chosen_door = random.randrange( len(doors) )
if verbose:
print("Le joueur choisi la porte",chosen_door)
for i in range(len(doors)):
if doors[i] == "chèvre" and i != chosen_door:
goat_door = i
if verbose:
print("\tLe présentateur ouvre la porte",goat_door)
break
if swap:
for i in range(len(doors)):
if i != goat_door and i != chosen_door:
chosen_door = i
if verbose:
print("\tLe joueur choisi de changer pour la porte",chosen_door)
break
if doors[chosen_door] == "voiture":
if verbose:
print("\tLe joueur gagne la voiture")
wins = wins + 1
else:
if verbose:
print("\tLe joueur perd et garde la chèvre")
return wins
if __name__ == "__main__":
verbose = False
runs = 100000
wins_swap = 0
wins_noswap = 0
for i in range(runs):
wins_noswap += play( False, verbose )
wins_swap += play( True, verbose )
print("Probabilité de gagner la voiture en changeant",wins_swap/runs,"et sans changer",wins_noswap/runs)