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

标签 python jupyter-notebook plotly

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

代码是使用 python 3 在 jupyter 中编写的,并使用plotly 呈现

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

问题 1: 为什么它没有返回正确的比例,应该是 33% 和 66%。目前约为 55% 和 44%。

问题 2: 如果您要使其更加精简但仍然非常基本,您会做什么?

问题 3: Secrets.randbelow(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 %

最佳答案

更优雅的编写方式应该是这样的:

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:secrets.randbelow绝对是足够随机的。对于这样的事情,它甚至可能有点矫枉过正,因为您不需要拥有加密的强随机数。因此,您还可以使用 numpy 的 random 或 python 中的“random”库。

关于python - 关于 Monty Hall 问题的程序未返回预期结果,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55170218/

相关文章:

python - 由于与 shell 进程的连接丢失,Visual Studio Code 重新启动终端

python - 为什么 pstats.print_callers() 忽略限制参数?

python - 使用 Django 扩展标签

python - Jupyter notebook 无法从 nb_conda_kernels 找到适用于 conda 环境的内核

javascript - plotly 中在文本中包含多个超链接

R:具有小倍数/子图的绘图纵横比

python - 如何获取非整数元素的索引

python - Jupyter Notebook 5.0 - 恢复旧表样式

python - 如何在 Python 中重新加载环境变量?

css - SVG隐藏的导航栏