montyhall.py

Created by schraf

Created on May 25, 2023

593 Bytes

Le problème de Monty Hall ou les probabilités changent de porte | Voyages au pays des maths | ARTE

Visitez ma chaine YouTube


from random import *

rester, changer = 0, 0
nb_simul = 10000

for _ in range(nb_simul):
 portes = {1, 2, 3}
 cadillac = choice(list(portes))
 choix_1 = choice(list(portes))
 portes.remove(choix_1)
 if choix_1 != cadillac: portes.remove(cadillac)
 chevre = choice(list(portes))
 portes = {1, 2, 3}
 portes.remove(choix_1)
 portes.remove(chevre)
 choix_2 = choice(list(portes))
 if choix_1 == cadillac: rester +=1
 if choix_2 == cadillac: changer +=1
print('Proba en restant : {:3f}'.format(rester / nb_simul))    
print('Proba en changeant : {:3f}'.format(changer / nb_simul))