monte carlo simulation

16 次查看(过去 30 天)
raymond
raymond 2011-11-21
can anyone please help me figure out whats wrong with this code. im trying to run a simulation where 3 people flip a coin if all are heads or tais they take them back. however if two coins come up heads or tails the person with the unique coin wins a coin from the other two. my problem is that my code doesnt follow the given conditions based on the code and instead seems to excute only one of the statements instead of following the given conditions
a=5; % person 1 int coin
b=5; % person 2 int coin
c=5; % person 3 int coin
step=0
for i=1:1:100
step=step+1
d=rand(1,3) % coin flip for 3 people
if d(1,1)&&d(1,2) < .5
a= a -1
b= b-1
c= c+2
elseif d(1,1)&&d(1,2) >.5
a= a -1
b= b-1
c= c+2
elseif d(1,1)&&d(1,3) < .5
a= a -1
b= b+2
c= c-1
elseif d(1,1)&&d(1,3) >.5
a= a -1
b= b+2
c= c-1
elseif d(1,3)&&d(1,2) < .5
a= a +2
b= b-1
c= c-1
elseif d(1,2)&&d(1,3) >.5
a= a +2
b= b-1
c= c-1
elseif d(1,1)&&d(1,2)&&d(1,3) >.5
a=a+0
b=b+0
c=c+0
elseif d(1,1)&&d(1,2)&&d(1,3) <.5
a=a+0
b=b+0
c=c+0
end
end
disp(d(1,1))
disp(d(1,2))
disp(d(1,3))

采纳的回答

Titus Edelhofer
Titus Edelhofer 2011-11-21
Hi Raymond,
your inequality conditions must be done each by itself: replace
if d(1,1)&&d(1,2)<.5
by
if d(1,1)<0.5 && d(1,2)<0.5
and likewise for the others. What you wrote is essentially
if d(1,1)~=0 && d(1,2)<.5
which is more or less the same as d(1,2)<0.5 since rand very seldom hits exactly zero.
Titus

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Monte-Carlo 的更多信息

标签

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by