The python source code for the Monte Carl of the Monty Hall Bet that I proposed. Feel free to comment or suggest improvements!

#!/usr/bin/env python # encoding: utf-8 """ MontyHall.py Created by Jonathan Whitmore on 2010-11-25. """ import sys import os import numpy as np import locale import random as ra import pylab as pl locale.setlocale(locale.LC_ALL, '') # Prints the dollar amounts pretty # -- if this is causing errors, comment it out SwitchBet = 1.08 # Value of bet that switching will pay StayBet = 1.00 # Value of bet that staying will pay Doors = 3 # How many doors are being offered Rounds = 10000 # How many rounds we go DoorArray = np.arange(Doors) + 1 #Holds the door number 1, 2, 3,... StayDoor = 1 # Initial door picked by both Stay and Switch NotStayDoor = DoorArray[np.where(DoorArray != StayDoor)] # Array of doors # that aren't the StayDoor print "SwitchBet is ",locale.currency(SwitchBet) print "StayBet is ",locale.currency(StayBet) print "Total Number of Doors: ", Doors print "Total Number of Rounds: ", Rounds SwitchBank = 0.0 # Set everything to zero StayBank = 0.0 # Set everything to zero SwitchWins = 0.0 # Set everything to zero StayWins = 0.0 # Set everything to zero StayWinRatio = [] # For statistical analysis SwitchWinRatio = [] # For statistical analysis for i in range(Rounds): WinningDoor = ra.choice(DoorArray) # Randomly choose door with prize # print "Round",i # print "The prize is behind door: ", WinningDoor RevealDoors = NotStayDoor[np.where(NotStayDoor != WinningDoor)] #Array of losing doors that are not the StayDoor # and not the WinningDoor RevealDoor = ra.choice(RevealDoors) # Randomly choose one from RevealDoors # print "Revealing Door: ", RevealDoor SwitchingOptions = NotStayDoor[np.where(NotStayDoor != RevealDoor)] SwitchDoor = ra.choice(SwitchingOptions) # print "Switching to Door: ", SwitchDoor if SwitchDoor == WinningDoor: SwitchWins += 1.0 # Add to number of Switch wins SwitchBank += StayBet # Add StayBet to SwitchBank StayBank -= StayBet # Subtract StayBet from StayBank if StayDoor == WinningDoor: StayWins += 1.0 # Add to number of Stay wins SwitchBank -= SwitchBet # Subtract SwitchBet from SwitchBank StayBank += SwitchBet # Add SwitchBet to StayBank StayWinRatio.append(StayWins/(i+1)) # Statistical Analysis SwitchWinRatio.append(SwitchWins/(i+1)) # Statistical Analysis print "Staying Percent Wins: ", round(StayWins/Rounds,4)*100,"%" print "Switching Percent Wins: ", round(SwitchWins/Rounds,4)*100,"%" print "The person who stays nets: ", locale.currency(StayBank) print "The person who switches nets: ", locale.currency(SwitchBank) |