Why are you flipping at the level of the "for i" loop, and then flipping again right the top of the "while" loop, without having tested the outcome of the flips?
Your code can be made simpler by using round(rand()) rather than rand(), as then you would have 0 and 1 values rather than values anywhere in the range [2^(-53) to 1-2^(-53)]
Your termination condition for the "while" loop contains redundancies. For example a>0 && c>0 is the same condition as (c>0&& a>0). And really all that you are testing there is that at least two people still have non-zero scores, which would be much more easily tested as ((a>0)+(b>0)+(c>0)) >= 2 . But I have to ask: why do you continue the game when someone goes broke?
What point does "d" have in the code? You initialize it to 0 and never change it.
After the change to 0/1 values, use abc as a vector of the scores, then your code for any one "experiment" reduces down to
abc = [5 5 5];
i = 0;
while all(abc) && i <= 10000
i = i + 1;
xyz = logical(round(rand(1,3)));
if all(xyz) || ~any(xyz)
else
xyz = xor(xyz, sum(xyz)==1);
abc(xyz) = abc(xyz) - 1;
abc(~xyz) = abc(~xyz) + 2;
end
end
disp([abc,i])
Best Answer