Skip to content

Instantly share code, notes, and snippets.

@mbiette
Created July 17, 2014 02:42
Show Gist options
  • Save mbiette/8f7e04876bb55eb90bf2 to your computer and use it in GitHub Desktop.
Save mbiette/8f7e04876bb55eb90bf2 to your computer and use it in GitHub Desktop.
Monty Hall game simulation
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="Encoding" useUTFGuessing="true" native2AsciiForPropertiesFiles="false" />
</project>
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="ProjectInspectionProfilesVisibleTreeState">
<entry key="Project Default">
<profile-state>
<expanded-state>
<State>
<id />
</State>
</expanded-state>
<selected-state>
<State>
<id>Buildout</id>
</State>
</selected-state>
</profile-state>
</entry>
</component>
<component name="ProjectRootManager" version="2" project-jdk-name="Python 3.3.0 (C:\Python33\python.exe)" project-jdk-type="Python SDK" />
</project>
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="ProjectModuleManager">
<modules>
<module fileurl="file://$PROJECT_DIR$/.idea/Monty Hall.iml" filepath="$PROJECT_DIR$/.idea/Monty Hall.iml" />
</modules>
</component>
</project>
<?xml version="1.0" encoding="UTF-8"?>
<module type="PYTHON_MODULE" version="4">
<component name="NewModuleRootManager">
<content url="file://$MODULE_DIR$" />
<orderEntry type="inheritedJdk" />
<orderEntry type="sourceFolder" forTests="false" />
</component>
</module>
<component name="DependencyValidationManager">
<state>
<option name="SKIP_IMPORT_STATEMENTS" value="false" />
</state>
</component>
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="VcsDirectoryMappings">
<mapping directory="" vcs="" />
</component>
</project>
__author__ = 'Maxime'
import random
def monty_hall():
# Setting up the game.
doors = (1, 2, 3)
prize_door = random.choice(doors)
# It is time to play, choose a door!
chose_door = random.choice(doors)
# Let open one of those door!
# This is the part were the host open one of the door that have a goat behind.
opened_door = random.choice(list(set(doors)-{prize_door, chose_door}))
# Do you wanna swap the door you chose?
swappable_door = list(set(doors)-{chose_door, opened_door})[0]
# Now the results.
win_if_kept_the_chosen_door = prize_door == chose_door
win_if_swapped_the_door = prize_door == swappable_door
return win_if_kept_the_chosen_door, win_if_swapped_the_door
__author__ = 'Maxime'
from MontyHall.MontyHall import monty_hall
def simulate_games(number_of_games):
count_win_kept = 0
count_win_swap = 0
while number_of_games > 0:
number_of_games -= 1
win_kept, win_swap = monty_hall()
if win_kept:
count_win_kept += 1
elif win_swap:
count_win_swap += 1
return {"win_kept":count_win_kept, "win_swap":count_win_swap}
__author__ = 'Maxime'
from MontyHall.Simulation import simulate_games
result = simulate_games(1000000)
print("Win % when keeping the same door:", result["win_kept"]/(result["win_kept"]+result["win_swap"]))
print("Win % when swapping the door:", result["win_swap"]/(result["win_kept"]+result["win_swap"]))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment