1

我正在编写一个脚本来证明 MATLAB 中的蒙蒂霍尔悖论,我有一个问题 - 我不知道如何让 MATLAB 生成一个与前两个不同的 1 到 3 之间的随机数。

这是我的脚本:

% This program demostrates the Monty Hall paradox.
% A_pick1 represents where person chooses to put the five-pound note
% B_pick1 represents which box person B  first identifies
% A_pick2 represents the box that person A opens up - it must be different
% than both A_pick1 and B_pick2

A_pick1 = round(3 * rand(1) + 0.5)
B_pick1 = round(3 * rand(1) + 0.5)
A_pick2 = round(3* rand(1) + 0.5);
while (A_pick2 == A_pick1 || A_pick2 == B_pick1)
   A_pick2 = rand(3 * rand(1) + 0.5) ~= (A_pick1 && B_pick1)
    break
end

我不确定如何正确使用 while 循环。

提前致谢

4

3 回答 3

3

使用randiand怎么样setxor

A_pick1 = randi(3);
B_pick1 = randi(3);

x = setxor(1:3,[A_pick1, B_pick1]);
A_pick2 = x(randi(numel(x)));

如果你有统计工具箱,还有其他选择,但我相信这应该做得很好。

祝你好运!

于 2014-04-03T13:23:59.360 回答
0

尝试

A_pick1 = round(3.0 * rand(1.0) + 0.5)

这样你就可以确保双倍。也许你想初始化你的随机数生成器rng,比如rng('shuffle');

于 2014-04-03T11:15:07.657 回答
0
while (A_pick2 == A_pick1 || A_pick2 == B_pick1)
   A_pick2 = rand(3 * rand(1) + 0.5);
end
于 2014-07-21T07:24:49.753 回答