Data commit
This commit is contained in:
parent
7387c8f97b
commit
cb5bb5e222
199093 changed files with 3378972 additions and 0 deletions
45
Task/Monty-Hall-problem/Python/monty-hall-problem-1.py
Normal file
45
Task/Monty-Hall-problem/Python/monty-hall-problem-1.py
Normal file
|
|
@ -0,0 +1,45 @@
|
|||
'''
|
||||
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
|
||||
|
||||
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(doorCount)] = True
|
||||
|
||||
chosen = door[choice]
|
||||
|
||||
unpicked = door
|
||||
del unpicked[choice]
|
||||
|
||||
# Out of those unpicked, the alternative 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 sum(monty_hall(randrange(3), switch=False)
|
||||
for x in range(iterations)),
|
||||
print "out of", iterations, "times."
|
||||
print "Switching allows you to win",
|
||||
print sum(monty_hall(randrange(3), switch=True)
|
||||
for x in range(iterations)),
|
||||
print "out of", iterations, "times.\n"
|
||||
33
Task/Monty-Hall-problem/Python/monty-hall-problem-2.py
Normal file
33
Task/Monty-Hall-problem/Python/monty-hall-problem-2.py
Normal file
|
|
@ -0,0 +1,33 @@
|
|||
import random
|
||||
#1 represents a car
|
||||
#0 represent a goat
|
||||
|
||||
stay = 0 #amount won if stay in the same position
|
||||
switch = 0 # amount won if you switch
|
||||
|
||||
for i in range(1000):
|
||||
lst = [1,0,0] # one car and two goats
|
||||
random.shuffle(lst) # shuffles the list randomly
|
||||
|
||||
ran = random.randrange(3) # gets a random number for the random guess
|
||||
|
||||
user = lst[ran] #storing the random guess
|
||||
|
||||
del(lst[ran]) # deleting the random guess
|
||||
|
||||
huh = 0
|
||||
for i in lst: # getting a value 0 and deleting it
|
||||
if i ==0:
|
||||
del(lst[huh]) # deletes a goat when it finds it
|
||||
break
|
||||
huh+=1
|
||||
|
||||
if user ==1: # if the original choice is 1 then stay adds 1
|
||||
stay+=1
|
||||
|
||||
if lst[0] == 1: # if the switched value is 1 then switch adds 1
|
||||
switch+=1
|
||||
|
||||
print("Stay =",stay)
|
||||
print("Switch = ",switch)
|
||||
#Done by Sam Witton 09/04/2014
|
||||
Loading…
Add table
Add a link
Reference in a new issue