- android - 多次调用 OnPrimaryClipChangedListener
- android - 无法更新 RecyclerView 中的 TextView 字段
- android.database.CursorIndexOutOfBoundsException : Index 0 requested, 光标大小为 0
- android - 使用 AppCompat 时,我们是否需要明确指定其 UI 组件(Spinner、EditText)颜色
我想做一些看似简单的事情,同时通过代码证明 Monty Hall,遗憾的是我得到的不是问题的证明,而是完全相反的。无论我是否切换,我在 10000 次模拟中获胜的几率约为 33%。你能检查一下代码,看看它有什么问题吗?请记住,我是一名网络开发人员,所以我的 Python 技能不是很好,而且代码可能看起来不太好(例如,我制作了一个 removable_doors 列表,其中只有一个变量就可以正常工作)。干杯!
import random
av_doors = [1, 2, 3]
door_v = [1, 2, 3]
removable_doors = [1, 2, 3]
score = 0
loses = 0
door = int(input("Choose your door (1-3)"))
pick = int(door)-1
dec = input("Stay or switch? (0/1)")
n = 0
while n < 10000:
removable_doors = [1, 2, 3]
av_doors = [1, 2, 3]
val = random.randint(1,3)
if val == 1:
door_v[0] = 1
door_v[1] = 0
door_v[2] = 0
removable_doors.remove(1)
elif val == 2:
door_v[0] = 0
door_v[1] = 1
door_v[2] = 0
removable_doors.remove(2)
elif val == 3:
door_v[0] = 0
door_v[1] = 0
door_v[2] = 1
removable_doors.remove(3)
try:
removable_doors.remove(int(door))
except:
pass
av_doors.remove(door)
if len(removable_doors) == 2:
rand_door = random.randint(0,1)
if rand_door == 0:
del av_doors[0]
elif rand_door ==1:
del av_doors[1]
else:
del av_doors[0]
if dec == "1":
door = av_doors[0]
else:
pass
pick = door-1
if door_v[pick] == 1:
score+=1
else:
loses+=1
n+=1
print(score)
print(loses)
最佳答案
您的房东有时会移开一扇门,后面有车...这是一个有效的解决方案,并且(我认为)写得更好,同时保持您的结构(在 python3 中,但这不应该引起麻烦):
import random
win = 0
lose = 0
switch = int(input("Stay or switch? (0-stay/1-switch):")) == 1
n = 0
while n < 10000:
# Init
door_values = [0, 0, 0]
removable_doors = [0, 1, 2]
available_doors = [0, 1, 2]
# Placing the reward somewhere
car_place = random.randint(0, 2)
door_values[car_place] = 1
removable_doors.remove(car_place)
# Choose a door
door_chosen = random.randint(0, 2)
available_doors.remove(door_chosen)
if door_chosen != car_place:
removable_doors.remove(door_chosen)
# Host removes a door that does not have the car and has not been chosen by the player
door_removed_by_host = removable_doors[random.randint(0, len(removable_doors)-1)]
available_doors.remove(door_removed_by_host)
# Switch if specified
if switch:
assert(len(available_doors) == 1)
door_chosen = available_doors[0]
# Check the result
if car_place == door_chosen:
win += 1
else:
lose += 1
n+=1
print('win=%s'%str(win))
print('lose=%s'%str(lose))
print('ratio=%s'%str(win/(win+lose)))
当 switch=False
时,它给了我 0.3332
,switch=True
时,它给了我 0.6738
,我猜这是相当证实了 Monty Hall 的解决方案:)
关于python - Monty Hall 模拟器有一个错误的结果,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43987782/
根据我读到的概率,切换门应该有大约 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
我是一名优秀的程序员,十分优秀!