Hello, I need help with editing this program:
#!/usr/bin/env python
"""
Monte Carlo simulation for the Monty Hall Problem:
http://en.wikipedia.org/wiki/Monty_Hall_problem.
"""
import sys
from random import randrange, shuffle, choice
DOORS = ['car', 'goat', 'goat']
def pick_door():
"""Return a number representing the player's first choice."""
return randrange(3)
def reveal_door(pick):
"""Return the index of the door opened by the host.
This cannot be a door hiding a car or the player's chosen door.
"""
all_doors = set([0, 1, 2])
unavailable_doors = set([DOORS.index('car'), pick])
available_doors = list(all_doors - unavailable_doors)
return choice(available_doors)
def staying_wins(pick):
"""Return True if the player won by staying
with their first choice, False otherwise.
"""
return won(pick)
def switching_wins(pick, open_door):
"""Return True if the player won by switching,
False otherwise.
"""
other_doors = set([pick, open_door])
switched_pick = (set([0, 1, 2]) - other_doors).pop()
return won(switched_pick)
def won(pick):
"""Return True if the player's final pick hides a car,
False otherwise.
"""
return (DOORS[pick] == 'car')
def main(iterations=1000000):
"""Run the main simulation as many
times as specified by the function argument.
"""
shuffle(DOORS)
switching = 0
staying = 0
for dummy in xrange(iterations):
picked = pick_door()
revealed = reveal_door(picked)
if staying_wins(picked):
staying += 1
if switching_wins(picked, revealed):
switching += 1
staying_rate = (float(staying) / iterations) * 100
switching_rate = (float(switching) / iterations) * 100
print "Staying: %f%%" % staying_rate
print "Switching: %f%%" % switching_rate
if __name__ == "__main__":
if len(sys.argv) == 2:
main(int(sys.argv[1]))
else:
main()
You can probably tell what this program does so I shall not elaborate. However, how can I edit the program so that it works for (for example) 4 goats and 1 car? Of course
DOORS = ['car', 'goat', 'goat']
needs to be changed but I'm not sure what else. Also, what does
return randrange(3)
do?
Thanks :)