Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import random
- def monty (numTrials):
- resultSwitch = 0
- resultStay = 0
- choices = ["Car", "Goat", "Goat"]
- for trial in range(numTrials):
- random.shuffle(choices)
- resultSwitch, resultStay = montyPick(choices,resultSwitch, resultStay)
- print "Out of %d trials run:" % numTrials
- print "Trials won by switching:", resultSwitch, "percent of total:", float(resultSwitch)*100/numTrials
- print "Trials won by staying:", resultStay, "percent of total:", float(resultStay)*100/numTrials
- def findInList(lst, target):
- for index in range(len(lst)):
- if lst[index] == target:
- return index
- return -1
- def montyPick (choices, resultSwitch, resultStay):
- pick = 0
- if choices[pick] == "Car": monty = 1
- else: monty = findInList(choices[1:], "Goat") + 1
- if monty == 1: remainingDoor = 2
- else: remainingDoor = 1
- montyChoice = choices[monty]
- if choices[remainingDoor] == "Car":
- resultSwitch += 1
- elif choices[pick] == "Car":
- resultStay += 1
- return resultSwitch, resultStay
- monty (100000)
Advertisement
Add Comment
Please, Sign In to add comment