From: <HIDDEN>
Newsgroups: comp.soft-sys.matlab
Subject: Re: Help with Monte Carlo Simulation
Date: Fri, 22 Oct 2010 21:41:03 +0000 (UTC)
Organization: The MathWorks, Inc.
Lines: 19
Message-ID: <i9t0df$ato$>
References: <i9sk00$ghf$>
Reply-To: <HIDDEN>
Content-Type: text/plain; charset=UTF-8; format=flowed
Content-Transfer-Encoding: 8bit
X-Trace: 1287783663 11192 (22 Oct 2010 21:41:03 GMT)
NNTP-Posting-Date: Fri, 22 Oct 2010 21:41:03 +0000 (UTC)
X-Newsreader: MATLAB Central Newsreader 1187260
Xref: comp.soft-sys.matlab:680676

"Rami " <> wrote in message <i9sk00$ghf$>...
> .....  If someone could help me out with what is wrong and any other advice it would be appreciated greatly. Thank you.
> ......
- - - - - - - - - - -
  I see three serious mistakes in your coding.  The first one is that the expressions "X(3,1)==X(2,1)~=X(1,1)", "X(1,1)==X(3,1)~=X(2,1)", "X(1,1)==X(2,1)~=X(3,1)", and "X(1,1)==X(2,1)==X(3,1)" are not acting the way you are intending them to act.  The first three are equivalent and are true whenever there is an even number of ones (trues) in the three X's.  The last one is true whenever there is an odd number of ones.  This means that half the time player Y1 wins two coins and the other half no-one wins, a decidedly unfair game.

  If a, b, and c are logical quantities, the expression a==b~=c is interpreted as (a==b)~=c and this has the effect of saying that an even number of them are true - either all are false, or else two of them true and the other one false.  Try all eight cases on your computer and see.  What I think you want for Y1 to win is (X(2)==X(3)) & (x(1)~=X(2)), and analogously for Y2 and Y3.  Also it is unnecessary to test for the case when no-one wins since the Y's will not change unless you tell them to change.

  The second error is that after the while-loop exits you don't store the count in 'c' anywhere.  Store it in an array indexed by the 'i' value in your for-loop.  You need this to compute the mean (average) value when you're ready to exit.

  The third error is that you don't restore the coin counts in the Y's when you begin a new run in the game.  You should move the Y initialization down into the beginning of the for-loop where you are already resetting c and j.  Otherwise on subsequent passes you will be starting with one player having no coins.  You could get negative coin counts this way.

  You will eventually learn that you can accomplish many things more efficiently by dealing with entire vectors.  For example in simulating a coin toss using the X's you could have done this:

 X = (rand(3,1)>p);

and it would accomplish what you have taken 18 lines to do.  (By the way, if p is not equal to 1/2, those must be rather strange coins!)

Roger Stafford