关于Monty-Hall问题的程序没有返回预期的结果

2024-10-03 13:21:33 发布

您现在位置:Python中文网/ 问答频道 /正文

首先,这个程序可能是愚蠢的、低效的、冗长的,但这是我第一个真正的程序,如果你建议对程序进行更改,请记住这一点。文本是挪威语。如果有什么不清楚的请直接问,我会翻译更多。

代码使用python3在jupyter中编写,并使用plotly

我读了this线程,因为它描述了我的问题,但我没有正确理解它,答案可能就在那里。

问题1: 为什么返回的比例不正确,应该是33%和66%。目前,约为55%和44%。你知道吗

问题2: 如果你想让它更精简,但仍然非常基本,你会怎么做?你知道吗

问题3: 是机密.randbown(3) “足够随机”以这种方式使用?你知道吗

问题4: 关于如何更好地呈现数据有什么建议吗?你知道吗

很抱歉事先给您带来了混乱的代码和拼写错误。如果代码是不可读的,我很乐意翻译更多的。你知道吗

import random     #importerer brukte pakker
import secrets
import plotly.plotly 
import plotly.graph_objs as go
from plotly.offline import download_plotlyjs, init_notebook_mode, plot,      iplot
import numpy
init_notebook_mode(connected=True)


dør1 = 0;         # initialising the variables
dør2 = 0;
dør3 = 0;
bytte_tap = 0  #Keeps track of how many loses after changing
bytte_vinn = 0 #Keeps track of how many wins after changing
bli_tap = 0    #Keeps track of how many loses after not changing
bli_vinn = 0   #Keeps track of how many wins after not changing
i = 0

print_on = 0          # Sett 1 for å få debug koder
antall_runder = 1000000  #sets amount of runs


def scenario_1(): # defines the three positions the car can be in
    global dør1   # 1 = Car 0 = Goat
    global dør2
    global dør3
    dør1 = 1
    dør2 = 0
    dør3 = 0


def scenario_2(): 
    global dør1   
    global dør2
    global dør3
    dør1 = 0
    dør2 = 1
    dør3 = 0


def scenario_3(): 
    global dør1   
    global dør2
    global dør3
    dør1 = 0
    dør2 = 0
    dør3 = 1


while i < antall_runder:  # main loop

    i += 1 # counter

    scenario_valg = secrets.randbelow(3) +1  # Chooses one of the possible positions 



    if scenario_valg == 1:     # Runs the chosen scenario.
        scenario_1()
    elif scenario_valg == 2:   # Runs the chosen scenario.
        scenario_2()
    elif scenario_valg == 3:   # Runs the chosen scenario.
        scenario_3()
    else:
        print("error")

    første_valg = secrets.randbelow(3) +1 # Randomly chooses the first door.

    andre_valg = secrets.randbelow(2)   # Randomly chooses whether the player chooses a new door


    if scenario_valg == 1 and første_valg == 1 and andre_valg == 1: # Figures out if the player has a correct combination of choices for scenario 1.
        if print_on == 1: print("1, 1, ja, tap")                    
        bytte_tap += 1
    elif scenario_valg == 1 and første_valg == 1 and andre_valg == 0:
        if print_on == 1: print("1, 1, nei, vinn")
        bli_vinn += 1
    elif scenario_valg == 1 and første_valg == 2 and andre_valg == 1:
        if print_on == 1: print("1, 2, ja, tap")
        bytte_tap += 1
    elif scenario_valg == 1 and første_valg == 2 and andre_valg == 0:
        if print_on == 1: print("1, 2, nei, vinn")
        bli_vinn += 1
    elif scenario_valg == 1 and første_valg == 3 and andre_valg == 1:
        if print_on == 1: print("1, 3, ja, vinn")
        bytte_vinn += 1
    elif scenario_valg == 1 and første_valg == 3 and andre_valg == 0:
        if print_on == 1: print("1, 3, nei, tap")
        bli_tap += 1



    if scenario_valg == 2 and første_valg == 1 and andre_valg == 1: # Figures out if the player has a correct combination of choices for scenario 2.
        if print_on == 1: print("2, 1, ja, vinn")                  
        bytte_vinn += 1
    elif scenario_valg == 2 and første_valg == 1 and andre_valg == 0:
        if print_on == 1: print("2, 1, nei, tap")
        bli_tap += 1
    elif scenario_valg == 2 and første_valg == 2 and andre_valg == 1:
        if print_on == 1: print("2, 2, ja, tap")
        bytte_tap += 1
    elif scenario_valg == 2 and første_valg == 2 and andre_valg == 0:
        if print_on == 1: print("2, 2, nei, vinn")
        bli_vinn += 1
    elif scenario_valg == 2 and første_valg == 3 and andre_valg == 1:
        if print_on == 1: print("2, 3, ja, vinn")
        bytte_vinn += 1
    elif scenario_valg == 2 and første_valg == 3 and andre_valg == 0:
        if print_on == 1: print("1, 3, nei, tap")
        bli_tap += 1


    if scenario_valg == 3 and første_valg == 1 and andre_valg == 1:  # Figures out if the player has a correct combination of choices for scenario 3.
        if print_on == 1: print("3, 1, ja, vinn")                    
        bytte_vinn += 1
    elif scenario_valg == 3 and første_valg == 1 and andre_valg == 0:
        if print_on == 1: print("3, 1, nei, tap")
        bli_tap += 1
    elif scenario_valg == 3 and første_valg == 2 and andre_valg == 1:
        if print_on == 1: print("3, 2, ja, vinn")
        bytte_vinn += 1
    elif scenario_valg == 3 and første_valg == 2 and andre_valg == 0:
        if print_on == 1: print("3, 2, nei, tap")
        bli_tap += 1
    elif scenario_valg == 3 and første_valg == 3 and andre_valg == 1:
        if print_on == 1: print("3, 3, ja, tap")
        bytte_tap += 1
    elif scenario_valg == 3 and første_valg == 3 and andre_valg == 0:
        if print_on == 1: print("3, 3, nei, vinn")
        bli_vinn += 1

init_notebook_mode()              # Plotly stuff i don't understand

keys=['Vinn - tap med bytting', 'Vinn - tap uten bytting']  # More Plotly stuff i don't understand
values=[bytte_vinn - bytte_tap, bli_vinn - bli_tap]

iplot({
    "data": [go.Bar(x=keys, y=values)],
    "layout": go.Layout(title="Monty Hall problemet")  # More Plotly stuff i don't understand
})

prosent_uten_bytting = bli_vinn / antall_runder * 100 *2  # Calculates the % of wins if you don't change your choice.
prosent_med_bytting = bytte_vinn / antall_runder * 100 *2 # Calculates the % of wins if you change your choice.



if print_on == 1: print(bytte_vinn, bytte_tap, bli_vinn, bli_tap)  # Debug message
print("Med bytting vant du", prosent_med_bytting, "% av tiden")   # Prints the %
print("Uten bytting vant du", prosent_uten_bytting, "% av tiden")# Prints the %

Tags: andoftheifontapscenarioprint
2条回答

更优雅的写作方式是这样的:

import numpy as np
cnt = 0
tries = 1000000
for _ in range(tries):
    doors = np.zeros(3)
    doors[np.random.randint(3)] = 1
    choice = np.random.randint(3)
    if doors[choice] == 1:  # If we chose this door on the first try we will change the door afterwards and not win
        cnt+=1

print("Lost:",cnt/tries)
print("Won:",(tries-cnt)/tries)

你基本上只需要一个反变量,在这里你要么数你赢了几轮,要么数你输了几轮。然后有一个循环,其中有两个随机数。我确实用了一个数组来表示门,但是你也可以用随机数来表示赢在哪扇门后面。你只需要一张支票。如果你选的门是奖品所在的门,你就会松开,因为版主打开一扇门,你就会切换到另一扇门(后面什么都没有)。如果你没有选择带奖品的门,你就赢了,因为你现在切换到带奖品的门。所以如果你不需要指纹,很多if语句就会消失。你知道吗

问题3:机密.randbown绝对是随机的。对于这样的事情,甚至可能有点过分,因为你不需要有加密的强随机数。因此,您还可以使用numpy的random或python中的“random”库。你知道吗

对于你的首要问题,33%和66%是否代表玩家在保持当前门或切换之间随机选择的情况?我以为那是不换和换的。无论哪种方式,这部分代码都可以做得更好:

if scenario_valg == 1 and første_valg == 1 and andre_valg == 1: # Figures out if the player has a correct combination of choices for scenario 1.
    if print_on == 1: print("1, 1, ja, tap")                    
    bytte_tap += 1
elif scenario_valg == 1 and første_valg == 1 and andre_valg == 0:
    if print_on == 1: print("1, 1, nei, vinn")
    bli_vinn += 1
elif scenario_valg == 1 and første_valg == 2 and andre_valg == 1:
    if print_on == 1: print("1, 2, ja, tap")
    bytte_tap += 1
elif scenario_valg == 1 and første_valg == 2 and andre_valg == 0:
    if print_on == 1: print("1, 2, nei, vinn")
    bli_vinn += 1
elif scenario_valg == 1 and første_valg == 3 and andre_valg == 1:
    if print_on == 1: print("1, 3, ja, vinn")
    bytte_vinn += 1
elif scenario_valg == 1 and første_valg == 3 and andre_valg == 0:
    if print_on == 1: print("1, 3, nei, tap")
    bli_tap += 1



if scenario_valg == 2 and første_valg == 1 and andre_valg == 1: # Figures out if the player has a correct combination of choices for scenario 2.
    if print_on == 1: print("2, 1, ja, vinn")                  
    bytte_vinn += 1
elif scenario_valg == 2 and første_valg == 1 and andre_valg == 0:
    if print_on == 1: print("2, 1, nei, tap")
    bli_tap += 1
elif scenario_valg == 2 and første_valg == 2 and andre_valg == 1:
    if print_on == 1: print("2, 2, ja, tap")
    bytte_tap += 1
elif scenario_valg == 2 and første_valg == 2 and andre_valg == 0:
    if print_on == 1: print("2, 2, nei, vinn")
    bli_vinn += 1
elif scenario_valg == 2 and første_valg == 3 and andre_valg == 1:
    if print_on == 1: print("2, 3, ja, vinn")
    bytte_vinn += 1
elif scenario_valg == 2 and første_valg == 3 and andre_valg == 0:
    if print_on == 1: print("1, 3, nei, tap")
    bli_tap += 1


if scenario_valg == 3 and første_valg == 1 and andre_valg == 1:  # Figures out if the player has a correct combination of choices for scenario 3.
    if print_on == 1: print("3, 1, ja, vinn")                    
    bytte_vinn += 1
elif scenario_valg == 3 and første_valg == 1 and andre_valg == 0:
    if print_on == 1: print("3, 1, nei, tap")
    bli_tap += 1
elif scenario_valg == 3 and første_valg == 2 and andre_valg == 1:
    if print_on == 1: print("3, 2, ja, vinn")
    bytte_vinn += 1
elif scenario_valg == 3 and første_valg == 2 and andre_valg == 0:
    if print_on == 1: print("3, 2, nei, tap")
    bli_tap += 1
elif scenario_valg == 3 and første_valg == 3 and andre_valg == 1:
    if print_on == 1: print("3, 3, ja, tap")
    bytte_tap += 1
elif scenario_valg == 3 and første_valg == 3 and andre_valg == 0:
    if print_on == 1: print("3, 3, nei, vinn")
    bli_vinn += 1

您可以进行更少的检查(从而编写更少的代码),而不是将每个选项分成每个可能的场景。我会的

    1. 检查第一个选择是否正确
    1. 检查repick

一旦您的场景被分成这4个选项,您就可以检查wins并直接打印变量scenario_valgførste_valg,而不是硬编码打印“1”和“2”。你知道吗

相关问题 更多 >