[ad_1]
I created a (rather complicated) Monty Hall simulation in Python, but, when run, returns equal odds of 33% for both Switching and not, when I know that, in reality, this cannot be the case and is not the case. What is wrong?
import math
import random
Right = 0
def TestWithSwitch():
global Right
wdoor = math.floor(random.random() * 3)
doors = [0,0,0]
doors[wdoor] = 1
Ldoors = [0,0]
i=0
##Declare winning door to be the winning door in the door Array
for x in range(0, 3):
if(x!=3):
if(doors[x] != 1):
Ldoors[i] = x
i+=1
##Chose the losing doors to be the doors that aren't the winning door
choice = math.floor(random.random() * 3)
DoorOut = 0
##Pick a Choice
LChose = False
for y in range(0, 2):
if(y!= 2):
if(Ldoors[y] == choice):
DoorOut = Ldoors[(y+1)%2]
LChose = True
if(LChose == False):
DoorOut = Ldoors[math.floor(random.random() * 2)]
Reserved = [DoorOut, choice]
##DoorOut is chosen from any of the losing doors we didn't pick as our choice, and is the door the computer is told doesn't have the prize
for z in range(0, 3):
if(z!= 3):
if(z in Reserved == False):
choice = z
##Make our new choice the other choice that we didn't previously choose
if(choice == wdoor):
Right+=1
def TestNoSwitch():
global Right
wdoor = math.floor(random.random() * 3)
doors = [0,0,0]
doors[wdoor] = 1
Ldoors = [0,0]
i=0
for x in range(0, 3):
if(x!=3):
if(doors[x] != 1):
Ldoors[i] = x
i+=1
choice = math.floor(random.random() * 3)
if(choice == wdoor):
Right+=1
for j in range(1, 10000):
## TestWithSwitch() and TestNoSwitch() both result in about 1/3. You can
test by putting either function in.
if(j == 9999):
print(Right/10000)
I know that switching should return 66% odds, while not should return 33% odds. THe odds I recieve tend to not even add up to 100% , but about two thirds, which is a probabilistic impossibility.
[ad_2]
لینک منبع