简体   繁体   English

Monty Hall模拟器无法正常工作

[英]Monty Hall simulator is not working properly

I tried to simulate Monty Hall problem. 我试图模拟Monty Hall问题。

First I choosed the winning door and randomized a selection. 首先,我选择了胜出的门并随机选择。 Then opened a door which is not the selected door. 然后打开一个不是所选门的门。 Then checked if selected = winning door. 然后检查是否选择了=胜出的门。 If it is, I added 1 to not switching point (b.num). 如果是这样,我在不切换点(b.num)上加了1。

Then to switch the selection, I removed selected door from the doors list and equalized selected to the only item in the list. 然后要切换选择,我从门列表中删除了选择的门,并将选择的门均等化为列表中的唯一项目。 Then checked if selected = winning door again and if it is I added 1 to switching point (s.num1). 然后检查是否已选择=再次赢得门,是否已将其添加到切换点(s.num1)中。

The problem is switching win percent is roughly equal 33. The expected value of it is about 66. Where is the problem? 问题是转换胜率大约等于33。其预期值约为66。问题出在哪里?

import random
from random import randint

def b():
    b.num+=1
b.num=0

def s():
    s.num+=1
s.num=0

d=int(input("try count: "))

def a():
    winner=randint(1,3)
    selection=randint(1,3)
    doors=[1,2,3]
    doors_to_open=randint(1,2)
    if selection == 1:
        if doors_to_open==1:
            doors.remove(2)
        else:
            doors.remove(3)

    if selection == 2:
        if doors_to_open==1:
            doors.remove(1)
        else:
            doors.remove(3)

    if selection == 3:
        if doors_to_open==1:
            doors.remove(1)
        else:
            doors.remove(2)

    if selection == winner:
        b()
        #print("Not switching win count: ",b.num)

    # switch

    doors.remove(selection)
    selection = doors[0]

    if selection == winner:
        s()
        #print("Switching win count: ",s.num)

for q in range (0,d):
    a()

#print("Switching win count: ",s.num)
print("Switching percent: ",(s.num/d)*100)
#print("Not switching win count: ",b.num)
print("Not switching percent: ",(b.num/d)*100)

input()

you have missed a not equal to operator while calling s function. 您在调用s函数时错过了一个不等于运算符。 if you add ! 如果添加! in the condition. 在这种情况下。 it will fix your simulation. 它会修复您的模拟。

import random
from random import randint

def b():
    b.num+=1
b.num=0

def s():
    s.num+=1
s.num=0

d=int(input("try count: "))

def a():
    winner=randint(1,3)
    selection=randint(1,3)
    doors=[1,2,3]
    doors_to_open=randint(1,2)
    if selection == 1:
        if doors_to_open==1:
            doors.remove(2)
        else:
            doors.remove(3)

    if selection == 2:
        if doors_to_open==1:
            doors.remove(1)
        else:
            doors.remove(3)

    if selection == 3:
        if doors_to_open==1:
            doors.remove(1)
        else:
            doors.remove(2)

    if selection == winner:
        b()
        #print("Not switching win count: ",b.num)

    # switch

    doors.remove(selection)
    selection = doors[0]

    if selection != winner: #Change is done in this line
        s()
        #print("Switching win count: ",s.num)

for q in range (0,d):
    a()

#print("Switching win count: ",s.num)
print("Switching percent: ",(s.num/d)*100)
#print("Not switching win count: ",b.num)
print("Not switching percent: ",(b.num/d)*100)

(thanks to Mark Meyer) the problem has solved. (感谢Mark Meyer)问题已解决。 That code was opening the winning door too. 该代码也打开了胜利之门。 Here is the proper code: 这是正确的代码:

import random
from random import randint

def a():
    a.sayı+=1
a.sayı=0

def b():
    b.sayı+=1
b.sayı=0

def c():
    kapılar=[1,2,3]
    cevap=randint(1,3)
    seçim=randint(1,3)

    #kapı aç
    kapılar1=[1,2,3]
    if seçim==cevap:
        kapılar1.remove(cevap)
    else:
            kapılar1.remove(cevap)
            kapılar1.remove(seçim)
    #kapılar1.remove(cevap)
    #kapılar1.remove(seçim)
    kapılar.remove(kapılar1[0])

    #dont switch
    if seçim==cevap:
        a()

    #switch
    kapılar.remove(seçim)
    seçim=kapılar[0]

    if seçim==cevap:
        b()

d=int(input("Deneme sayısı: "))

for e in range(0,d):
    c()

print("Switching win percent: ",(b.sayı/d)*100)
print("Not switching win pecent: ",(a.sayı/d)*100)
input()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM