Monty Hall problem
Run random simulations of the Monty Hall game. Show the effects of a strategy of the contestant always keeping his first guess so it can be contrasted with the strategy of the contestant always switching his guess.
![Task](http://static.miraheze.org/rosettacodewiki/thumb/b/ba/Rcode-button-task-crushed.png/64px-Rcode-button-task-crushed.png)
You are encouraged to solve this task according to the task description, using any language you may know.
A well-known statement of the problem was published in Parade magazine:
- Suppose you're on a game show, and you're given the choice of three doors: Behind one door is a car; behind the others, goats. You pick a door, say No. 1, and the host, who knows what's behind the doors, opens another door, say No. 3, which has a goat. He then says to you, "Do you want to pick door No. 2?" Is it to your advantage to switch your choice? (Whitaker 1990)
Simulate at least a thousand games using three doors for each strategy and show the results in such a way as to make it easy to compare the effects of each strategy.
Python
<python> I could understand the explanation of the Monty Hall problem but needed some more evidence
References:
http://www.bbc.co.uk/dna/h2g2/A1054306 http://en.wikipedia.org/wiki/Monty_Hall_problem especially: http://en.wikipedia.org/wiki/Monty_Hall_problem#Increasing_the_number_of_doors
from random import randrange, shuffle
doors, iterations = 3,100000 # could try 100,1000
def monty_hall(choice, switch=False, doorCount=doors):
# Set up doors door = [False]*doorCount # One door with prize door[randrange(0, doorCount)] = True
chosen = door[choice]
unpicked = door del unpicked[choice]
# Out of those unpicked, the alterantive is either: # the prize door, or # an empty door if the initial choice is actually the prize. alternative = True in unpicked
if switch: return alternative else: return chosen
print "\nMonty Hall problem simulation:" print doors, "doors,", iterations, "iterations.\n"
print "Not switching allows you to win", print [monty_hall(randrange(3), switch=False)
for x in range(iterations)].count(True),
print "out of", iterations, "times." print "Switching allows you to win", print [monty_hall(randrange(3), switch=True)
for x in range(iterations)].count(True),
print "out of", iterations, "times.\n" </python> Sample output:
Monty Hall problem simulation: 3 doors, 100000 iterations. Not switching allows you to win 33337 out of 100000 times. Switching allows you to win 66529 out of 100000 times.