我正在尝试在Matlab中模拟Monty Hall问题。我究竟做错了什么?

时间:2019-11-12 19:10:07

标签: matlab statistics

如标题中所述。我知道在这个问题上如果有人调换门而获胜的可能性应该大约为66%,但是我似乎得到的答案暗示,在球员调换和球员不调换的情况下,成功率为33%。与这两个值关联的变量是swapwinratio和noswapwinratio,它们在运行时都始终返回约0.33的值。在测试中,我运行的样本数量为5000至10,000。这是我的代码:

clc; clear all; close all;

numsamps = input("Enter number of samples to take. \n");

if(mod(numsamps, 1) || numsamps < 1)
    error("Must be a positive integer number.");
end

wins = 0;
swapwins = 0;
noswapwins = 0;
swaps = 0;
noswaps = 0;
for i = 1:numsamps
    doors = 1:3;
    host = ceil(3 * rand());
    player = ceil(3 * rand());
    p1 = player;
    doors(player) = 0;
    goat = ceil(2 * rand());
    for j = goat:3
        if(doors(j) ~= 0)
            doors(j) = 0;
            break;
        end
    end
    swap = floor(2 * rand());
    if(swap)
        ind = find(doors ~= 0);
        player = doors(ind);
        swaps = swaps + 1;
    else
        noswaps = noswaps + 1;
    end
    if(player == host)
        wins = wins + 1;
        if(swap)
            swapwins = swapwins + 1;
        else
            noswapwins = noswapwins + 1;
        end
    end
end

swapwinratio = swapwins/swaps;
noswapwinratio = noswapwins/noswaps;

对不起,如果我错过任何事情;这是我第一次发布到stackoverflow!

0 个答案:

没有答案