- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
首先,这个程序可能愚蠢、低效且冗长,但这是我的第一个真正的程序,如果您建议对程序进行更改,请记住这一点。文本是挪威语。如果有任何不清楚的地方请询问,我会翻译更多。
代码是使用 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/
根据我读到的概率,切换门应该有大约 66% 的机会选择正确的门。下面这段代码是我想出的,它吐出大约 50% 的胜利,而不是我期望的 66%。任何关于我在这里出错的地方的帮助将不胜感激。 for (in
#include #include int main (void) { int pickedDoor, remainingDoor, hostDoor, winningDoor, opti
我想做一些看似简单的事情,同时通过代码证明 Monty Hall,遗憾的是我得到的不是问题的证明,而是完全相反的。无论我是否切换,我在 10000 次模拟中获胜的几率约为 33%。你能检查一下代码,看
我一直在尝试解决 monty hall problem为了提高编码水平,我使用Python,这就是为什么我尝试随机化一切。问题是:我遇到了一些麻烦。你们大多数人可能都知道蒙蒂问题应该表明改变门的胜率(
import java.util.Scanner; import static java.lang.System.*; import static java.lang.Math.*;
首先,这个程序可能愚蠢、低效且冗长,但这是我的第一个真正的程序,如果您建议对程序进行更改,请记住这一点。文本是挪威语。如果有任何不清楚的地方请询问,我会翻译更多。 代码是使用 python 3 在 j
下面的代码是我的实现(Python 3.3.1),看看我能否证明开关在蒙提霍尔问题中是有效的。当我让玩家保留他们的第一选择时,我大致得到大约 0.33 的正确猜测结果,这是预期的。当我切换播放器时会出
我刚刚为 Monty Hall 问题创建了一个模拟,但我的结果(即使有 10,000,000 次测试)还是很奇怪。对于策略 1(保持),命中率为 1/3,而策略 2(转换)为 44.44%。代码有错误
我正在尝试模拟 Monty Hall Problem (因为我从书中读到 Think Statistics 一个人只有在看到计算机模拟后才会被说服)C#,我最熟悉的编程语言。我的场景是奖品的位置是随机
我正在 R 中编写一个函数来对 Monty Hall 问题执行蒙特卡罗模拟。当门没有开关时,该函数正在工作 switch == FALSE,但是当我调用 mean(replicate(10000, m
from random import randint numberOfDoors = 3 success = 0 attempts = 0 while True: try: d
我哪里做错了吗?即使在切换后我仍然保持 50% 的胜利。 import random def monty_hall(): #-----setup ----# prizes = ["Ca
我正在尝试计算一个人在 10,000 次迭代中有多少次改变他们的门选择可能会获胜(得到汽车)。我不确定我的范围或计数器的格式是否不正确(或两者)。在我尝试打印计数器之前,代码似乎一直在工作——这导致了
所以我一直在尝试用 Python 来模拟一个类似于 Monty Hall 问题的问题,我预计转换后会产生 66% 的获胜结果,但事实并非如此。有人可以指出我哪里出了问题吗?我试图根据转换来得出获胜和失
只是为了好玩和训练 R,我试图证明 Monty Hall 游戏规则(打开一扇门后改变你的选择会让你更有可能获胜),我制作了这个可重现的代码(每一步的解释都在代码中): ## First I set t
我正在尝试理解 Monty Hall 问题的解决方案,我理解大部分代码,但卡在两部分上。 下面是代码,但具体来说我被困在这两部分 result[bad] = np.random.randint(0,3
关闭。这个问题是opinion-based .它目前不接受答案。 想要改进这个问题吗? 更新问题,以便 editing this post 提供事实和引用来回答它. 关闭 11 个月前。 Improv
我正在尝试重新创建 Monty Hall 游戏 ( here is a description of the game)它会要求用户输入一个数字(我的代码要求“输入随机种子”)开始,如果用户不输入数字
#include #include int main(void) { static int games = 0; static int stayWins = 0; stat
这更像是一种好奇心练习...... 如果您还没有听说过 The Monty Hall problem,那么这个很棒的 youtube video 中对此进行了解释. 我使用 numpy 在 pytho
我是一名优秀的程序员,十分优秀!