我想模拟以下内容:
我们有两支排球队:
L =本地和 V =客人
假设L有18个点,p1 = 0.6的概率得分为下一个点,而V有20个点,p2 = 0.54得分的概率为下一个点。
到目前为止我尝试的是这段代码: -
% Simulating a volleyball game
p1 = 0.6; % Introducing the probabilities of both teams
p2 = 0.54;
n=25; % The maximum number of points in a set
L=18;
V=20;
t1=(rand<p1);
t2=(rand<p2);
while n-L>0 || n-V>0
if t1==1
L=L+1;
elseif t2==1
V=V+1;
end
end
disp([L V])
事情是它似乎没有尽头,当球队得到25分时,我无法想出一种停止比赛的方法。另一个问题是我无法检查循环是否正常工作,因为程序没有显示任何内容。我认为该程序没有计算正确的t1
和t2
,因为它使用相同的随机数进行计算。
如何解决这些问题?
修改
另外,我想运行这10,000次并计算每个团队的胜率!
答案 0 :(得分:2)
首先,循环的条件不正确。其次,在每次迭代中,t1
和t2
的值都会不断增加。第三,当t1
和t2
都为零时,没有什么可以解决这种情况,循环将始终保持为真。第四,假设概率是错误的,因为概率之和必须等于1.
带有更正的修改后的代码如下:
% The probabilities are incorrect but I'm leaving them as it is
p1 = 0.6;
p2 = 0.54;
n = 25; % The maximum number of points in a set
Result=zeros(1,1000); % Pre-allocation
for k=1:1000
L=18;
V=20;
while n-L>0 & n-V>0 % Notice the condition
t1 = (rand<p1);
t2 = (rand<p2);
if t1==1
L=L+1;
elseif t2==1
V=V+1;
end
end
Result(k)=L>V ; % Storing L's victories as 1 and V's victories as 0
end
Win_Percent_L = length(find(Result))/1000
Win_Percent_V = 1-Win_Percent_L