Monty Hall problem: Difference between revisions

m
Undo revision 20559 by 62.123.142.25 (Talk) -I don't think it is improved by the capitalisation of constants or the #! line.
(Forth)
m (Undo revision 20559 by 62.123.142.25 (Talk) -I don't think it is improved by the capitalisation of constants or the #! line.)
Line 550:
=={{header|Python}}==
<python>
'''
#! /usr/bin/env 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
 
DOORSdoors, ITERATIONSiterations = 3, 100000 # could try 100, 1000
 
def monty_hall(choice, switch=False, doorCount=doors):
doors = ['goat'] * DOORS # Set up doors
door = [False]*doorCount
doors[randrange(DOORS)] = 'car' # One door with prize
chosen = doors[choice]
door[randrange(0, doorCount)] = True
unpicked = doors
 
del unpicked[choice]
chosen = doorsdoor[choice]
# Out of those unpicked, the alternative is either:
 
# the prize door, or
unpicked = doorsdoor
# an empty door if the initial choice is actually the prize.
if 'car' indel unpicked:[choice]
 
alternative = 'car'
# Out of those unpicked, the alternative is either:
else:
# the prize door, or
alternative = 'goat'
# an empty door if the initial choice is actually the prize.
if switch:
alternative = True in unpicked
return alternative
 
else:
if switch:
return chosen
return alternative = 'car'
else:
return alternativechosen
 
print "\nMonty Hall problem simulation:"
print DOORSdoors, "doors,", ITERATIONSiterations, "iterations.\n"
 
print "Not switching allows you to win",
print [monty_hall(randrange(DOORS3), switch=False)
for x in xrangerange(ITERATIONSiterations)].count('car'True),
print "out of", ITERATIONSiterations, "times."
print "Switching allows you to win",
print [monty_hall(randrange(DOORS3), switch=True)
for x in xrangerange(ITERATIONSiterations)].count('car'True),
print "out of", ITERATIONSiterations, "times.\n"
</python>
Sample output:
Anonymous user