monte carlo simulation

9 views (last 30 days)
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))

Accepted Answer

Titus Edelhofer
Titus Edelhofer on 21 Nov 2011
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

More Answers (0)

Community Treasure Hunt

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

Start Hunting!