广义Monty Hall问题的Matlab仿真程序
楼主 · 1 楼
%广义Monty
Hall问题的Matlab仿真程序
---------------------------------------------------
function [ ] = GMontyHall( a,b,n ) %广义Monty Hall问题(a辆汽车、b只羊)
index1=0; % 不改变选择得到汽车的次数,初值为0
index2=0; % 改变选择得到汽车的次数,初值为0
for i=1:n
car=unidrnd(a+b,1,a);% 随机生成a辆汽车所在的房间号(car(1)...car(a))
while allnotequ(car)==0 %
car=unidrnd(a+b,1,a);
end
choice1=unidrnd(a+b);% 随机选择一个房间号(即参赛者的第一次选择)
if belongto(choice1,car)==0 %
car2=unidrnd(a+b-2,1,a);
while allnotequ(car2)==0 %
car2=unidrnd(a+b-2,1,a);
end
choice2=unidrnd(a+b-2);% 随机选择一个房间号(参赛者的第2次选择)
if belongto(choice2,car2)==1
index2=index2+1;
end
else % 如果第一次选择的是车...
index1=index1+1; % 则不换必得车...
car3=unidrnd(a+b-2,1,a-1);
while allnotequ(car3)==0
car3=unidrnd(a+b-2,1,a-1);
end
choice3=unidrnd(a+b-2);% 随机选择一个房间号(参赛者的第2次选择)
if belongto(choice3,car3)==1
index2=index2+1;
end
end
end
p1=index1/n;
p2=index2/n;
fprintf('\n****** The solution of the general Monty Hall problem ****** \n');
fprintf('There are %2d cars and %2d sheep, total test number n= %5d.\n', a,b,n);
fprintf('Number of gaining the car without change the first choice = %5d.\n', index1);
fprintf('Number of gaining the car by changing the first choice = %5d.\n', index2);
fprintf('Probability of gaining the car without change the first choice = %6.4f.\n', p1);
fprintf('Probability of gaining the car by changing the first choice = %6.4f.\n', p2);
function [t]=allnotequ( car )
%用来判断行向量car的元素是否全部不相等,是返回1,否则0
n=size(car);% n 是一个行向量
E=ones(n);
for i=1:n(2)
A=car-car(i)*E;
for j=1:n(2)
if A(j)==0
for k=j+1:n(2)
if A(k)==0
t=0;
return
end
end
end
end
end
t=1;
function [t]=belongto(choice, car)
% 用来判断常数choice是不是行向量car的一个元素,是返回1,否则返回0
n=size(car);
for i=1:n(2)
if car(i)==choice % 此时choice 是car的一个元素
t=1;
return
end
end
t=0;
---------------------------------------------------
function [ ] = GMontyHall( a,b,n ) %广义Monty Hall问题(a辆汽车、b只羊)
index1=0; % 不改变选择得到汽车的次数,初值为0
index2=0; % 改变选择得到汽车的次数,初值为0
for i=1:n
car=unidrnd(a+b,1,a);% 随机生成a辆汽车所在的房间号(car(1)...car(a))
while allnotequ(car)==0 %
car=unidrnd(a+b,1,a);
end
choice1=unidrnd(a+b);% 随机选择一个房间号(即参赛者的第一次选择)
if belongto(choice1,car)==0 %
car2=unidrnd(a+b-2,1,a);
while allnotequ(car2)==0 %
car2=unidrnd(a+b-2,1,a);
end
choice2=unidrnd(a+b-2);% 随机选择一个房间号(参赛者的第2次选择)
if belongto(choice2,car2)==1
index2=index2+1;
end
else % 如果第一次选择的是车...
index1=index1+1; % 则不换必得车...
car3=unidrnd(a+b-2,1,a-1);
while allnotequ(car3)==0
car3=unidrnd(a+b-2,1,a-1);
end
choice3=unidrnd(a+b-2);% 随机选择一个房间号(参赛者的第2次选择)
if belongto(choice3,car3)==1
index2=index2+1;
end
end
end
p1=index1/n;
p2=index2/n;
fprintf('\n****** The solution of the general Monty Hall problem ****** \n');
fprintf('There are %2d cars and %2d sheep, total test number n= %5d.\n', a,b,n);
fprintf('Number of gaining the car without change the first choice = %5d.\n', index1);
fprintf('Number of gaining the car by changing the first choice = %5d.\n', index2);
fprintf('Probability of gaining the car without change the first choice = %6.4f.\n', p1);
fprintf('Probability of gaining the car by changing the first choice = %6.4f.\n', p2);
function [t]=allnotequ( car )
%用来判断行向量car的元素是否全部不相等,是返回1,否则0
n=size(car);% n 是一个行向量
E=ones(n);
for i=1:n(2)
A=car-car(i)*E;
for j=1:n(2)
if A(j)==0
for k=j+1:n(2)
if A(k)==0
t=0;
return
end
end
end
end
end
t=1;
function [t]=belongto(choice, car)
% 用来判断常数choice是不是行向量car的一个元素,是返回1,否则返回0
n=size(car);
for i=1:n(2)
if car(i)==choice % 此时choice 是car的一个元素
t=1;
return
end
end
t=0;