- android - 多次调用 OnPrimaryClipChangedListener
- android - 无法更新 RecyclerView 中的 TextView 字段
- android.database.CursorIndexOutOfBoundsException : Index 0 requested, 光标大小为 0
- android - 使用 AppCompat 时,我们是否需要明确指定其 UI 组件(Spinner、EditText)颜色
所以我一直在尝试用 Python 来模拟一个类似于 Monty Hall 问题的问题,我预计转换后会产生 66% 的获胜结果,但事实并非如此。有人可以指出我哪里出了问题吗?我试图根据转换来得出获胜和失败的百分比。任何帮助将不胜感激。
import random
#Ask the user for how many runs
runs = int(input("How many games do you want to simulate?"))
switchwins, nonswitchwins, switchlosses, nonswitchlosses = 0, 0, 0, 0
# Get the random number started with a seed
random.seed()
#run once for user switching and once for user not switching
for swap in True,False:
# Do everything for the number of runs we have
for i in range(runs):
# Setup the prize locations
prizes = ['nothing', 'nothing', 'coin']
# Randomly mix them up
random.shuffle(prizes)
#select a random location
ChoiceA = random.randrange(3)
# print("Before the prize is revealed, I will show you what is in one of the other hiding places")
# remove one of the other hiding places which has nothing as a prize and isn't ChoiceA
for currentlocation, contents in enumerate(prizes):
if currentlocation != ChoiceA and contents == "nothing":
showlocation = currentlocation
# print("There is nothing in this location", showlocation)
break
if swap:
#swap to the other location
for currentlocation, contents in enumerate(prizes):
if currentlocation != ChoiceA and contents != showlocation:
swap_to = currentlocation
# check if the swapped choice is a win
if prizes[swap_to] == "coin":
switchwins +=1
else:
switchlosses +=1
# when not swapping locations check for win
else:
if prizes[ChoiceA] == "coin":
nonswitchwins +=1
else:
nonswitchlosses +=1
print("This is the number of wins if the user switched",
round((switchwins/runs)*100,1), "%")
print("This is the number of wins if the user didn't switch",
round((nonswitchwins/runs)*100,1),"%")
最佳答案
你写了if currentlocation != ChoiceA andcontents != showlocation:
,但这不是if currentlocation != ChoiceA and currentlocation != showlocation:
吗?
关于python - Monty Hall 问题在切换和不切换时返回 33% 的结果,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53366172/
根据我读到的概率,切换门应该有大约 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
我是一名优秀的程序员,十分优秀!