SeSAC - 파이썬 몬티 홀 문제(Monti Hall problem)
          2023, Aug 11
        
      Monti Hall Problem
import random
import matplotlib.pyplot as plt
simulation = 1000000 # 10만회 시행
stay = 0
change = 0
chart = []
for _ in range(simulation):
    user_choice = random.randint(1, 3) # 유저의 선택
    doors = [1, 2, 3]
    car = random.choice(doors) # 차는 문들 중 랜덤한곳에 들어있다.
    monti = [door for door in doors if door != user_choice and door != car][0] # 몬티의 선택은 유저가 선택한것, 차가 들어있는 문을 제외한 하나.
    doors.remove(monti) # 몬티가 선택한 문은 더이상 고려 대상이 아니므로 제외된다.
		# 유저의 선택이 차가 들어있는 문이라면 
		# 바꾸지 않기 전략이 성공
    if user_choice == car:
        stay += 1
		# 바꾸기 전략, 문들 중 유저가 선택한 문이 아니며, 몬티가 선택한 문이 아닌것으로 변경
    changed = [door for door in [1, 2, 3] if door != user_choice and door != monti][0]
		# 바꾸기 전략이 차가 들어있는 문이라면
	  # 바꾸기 전략이 성공
    if changed == car:
        change += 1
    stay_percent = change / simulation * 100  # 바꾸지 않았을 때의 확률
    change_percent = stay / simulation * 100  # 바꿨을 때의 확률
    chart.append([stay_percent, change_percent])
chart = list(zip(*chart))  # 퍼센트 데이터를 분리
plt.plot(chart[0], label="Stay Strategy")
plt.plot(chart[1], label="Change Strategy")
plt.xlabel("Count")
plt.ylabel("Percent")
plt.title("Monty Hall Problem")
plt.show()
print("바꾸지 않았을 때의 평균 확률: {:.2f}%".format(stay_percent))
print("바꿨을 때의 평균 확률: {:.2f}%".format(change_percent))
바꾸지 않았을 때의 평균 확률: 66.67%
바꿨을 때의 평균 확률: 33.33%-