关于Monty Hall问题的计划未返回预期结果



首先,这些程序可能是愚蠢的效率效率低下,但这是我的第一个真实程序,如果您建议在程序中进行更改,请记住这一点。文字在挪威语中。如果有什么尚不清楚的话,请问,我会翻译更多。

该代码是使用Python 3编写的,并使用Plotly

呈现

我读了这个线程,因为它描述了我的问题,但我不正确理解,答案可能存在。

问题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绝对足够随机。这样的事情甚至可能有点过分,因为您不需要具有密码的强随机数。因此,您也可以使用Python的Numpy随机或"随机"库。

对于您的主要问题,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个选项,您可以检查获胜并打印变量scenario_valgførste_valg,而不是" 1"one_answers" 2"。

最新更新