Created
August 5, 2020 18:21
-
-
Save lemairecarl/1d476002e6f057db638136f43b97c6df to your computer and use it in GitHub Desktop.
Chèvres
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
def jouer(strategie): | |
porte_gagnante = np.random.randint(0, 3) | |
portes = {0, 1, 2} | |
choix_initial = np.random.randint(0, 3) | |
if choix_initial == porte_gagnante: | |
chevres = portes.difference({porte_gagnante}) | |
ouverte_par_animateur = list(chevres)[np.random.randint(0, 2)] | |
else: # choix_initial != porte_gagnante, choix_initial est chevre | |
# L'animateur n'ouvre ni la porte gagnante, ni la porte choisie initialement | |
ouverte_par_animateur = list(portes.difference({porte_gagnante, choix_initial}))[0] | |
portes_restantes = portes.difference({ouverte_par_animateur}) | |
if strategie == 'changer': | |
choix_final = list(portes_restantes.difference({choix_initial}))[0] | |
elif strategie == 'garder': | |
choix_final = choix_initial | |
elif strategie == 'hasard': | |
choix_final = list(portes_restantes)[np.random.randint(0, 2)] | |
else: | |
print('hein?') | |
return choix_final == porte_gagnante | |
def comparer(n): | |
for strategie in ['changer', 'garder', 'hasard']: | |
resultats = [1 if jouer(strategie) else 0 for _ in range(n)] | |
print(strategie, sum(resultats)) | |
# In [55]: comparer(100000) | |
# changer 66690 | |
# garder 33200 | |
# hasard 50160 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment