MATLAB: I’m asked to find the probability of this problem :(

probability

hello all, we had this problem in the class and we asked to verify the answer using MATLAB (answer=0.13867), so I write the following code:
if true
>> a= rand;
b=rand;
c=rand;
e=rand;
d=rand;
x=0.251;
n=0;
p=0;
for i=1: 10000
if (a<x)
a=1;
else
a=0;
end
if (b<x)
b=1;
else
b=0;
end
if (c<x)
c=1;
else
c=0;
end
if (d<x)
d=1;
else
d=0;
end
if (e<x)
e=1;
else
e=0;
end
p = (a*b)+ (a*e*d)+(c*d)+(c*e*b);
if (p>=1)
n=n+1;
end
end;
n/10000 end
and the answer appears is either 1 or 0.5, what is wrong? thanks ahead, I've to submitting it tomorrow, pls help

Best Answer

A few things...
- You're not changing anything within your loop. You probably want to be changing the values of a,b,c,d, and e on each iteration.
- You can replace each if statement with a = double(a < x).
- You can elimate the whole loop and do all the comparisons at once:
npt = 10000;
x = 0.251;
abcde = rand(npt,5);
isless = num2cell(double(abcde < x), 1);
[a,b,c,d,e] = deal(isless{:});
p = a.*b + a.*e.*d + c.*d + c.*e.*b;
frac = sum(p >= 1)/npt;
Related Question