Discover MakerZone

MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi

Learn more

Discover what MATLAB® can do for your career.

Opportunities for recent engineering grads.

Apply Today

Thread Subject:
Help with Monte Carlo Simulation

Subject: Help with Monte Carlo Simulation

From: Rami

Date: 22 Oct, 2010 18:09:04

Message: 1 of 3

Hi, I'm pretty new to matlab and I need to program a Monte Carlo simulation where 3 players have a number of coins. They decide to play a game where they flip coins and if the result ends up as 2 heads/tails and 1 tails/heads, the person with the 1 tails/heads result takes one coin from the other 2 player and keeps his or her coin. If the result is 3 heads or 3 tails then there is no change in any players coins. I need to figure out the average number of coin flips it takes to make one player go to zero. So far this is the code and I haven't tried to compute the average yet but my problem is that if I make n>1 then sometimes the code won't compute and it gets stuck at either "if X(3,1)==X(2,1)~=X(1,1)" which is line 29 or at line 50 which is just an "end" statement. If someone could help me out with what is wrong and any other advice it would be appreciated greatly. Thank you.

function avgflips = going_broke(initP1, initP2, initP3, p, n)
Y=zeros(3,1);
Y(1,1)=initP1;
Y(2,1)=initP2;
Y(3,1)=initP3;
X=zeros(3,1);
for i=1:n
    c=1;
    j=1;
    while j>0
        X(1,1)=rand;
        if X(1,1)>p
            X(1,1)=1;
        elseif X(1,1)<p
            X(1,1)=0;
        end
        X(2,1)=rand;
        if X(2,1)>p
            X(2,1)=1;
        elseif X(2,1)<p
            X(2,1)=0;
        end
        X(3,1)=rand;
        if X(3,1)>p
            X(3,1)=1;
        elseif X(3,1)<p
            X(3,1)=0;
        end
        if X(3,1)==X(2,1)~=X(1,1)
            Y(1,1)=Y(1,1)+2;
            Y(2,1)=Y(2,1)-1;
            Y(3,1)=Y(3,1)-1;
        elseif X(1,1)==X(3,1)~=X(2,1)
            Y(1,1)=Y(1,1)-1;
            Y(2,1)=Y(2,1)+2;
            Y(3,1)=Y(3,1)-1;
        elseif X(1,1)==X(2,1)~=X(3,1)
            Y(1,1)=Y(1,1)-1;
            Y(2,1)=Y(2,1)-1;
            Y(3,1)=Y(3,1)+2;
        elseif X(1,1)==X(2,1)==X(3,1)
            Y(1,1)=Y(1,1);
            Y(2,1)=Y(2,1);
            Y(3,1)=Y(3,1);
        end
        if Y(1,1)==0
            j=0;
        elseif Y(2,1)==0
            j=0;
        elseif Y(3,1)==0
            j=0;
        end
        c=c+1;
    end
end

Subject: Help with Monte Carlo Simulation

From: Roger Stafford

Date: 22 Oct, 2010 21:41:03

Message: 2 of 3

"Rami " <akatsukiX2@gmail.com> wrote in message <i9sk00$ghf$1@fred.mathworks.com>...
> ..... 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

Subject: Help with Monte Carlo Simulation

From: Rami

Date: 23 Oct, 2010 04:28:03

Message: 3 of 3

"Roger Stafford" <ellieandrogerxyzzy@mindspring.com.invalid> wrote in message <i9t0df$ato$1@fred.mathworks.com>...
> "Rami " <akatsukiX2@gmail.com> wrote in message <i9sk00$ghf$1@fred.mathworks.com>...
> > ..... 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

Ya I realized the problem about both not restoring the Y's and the fact that I made p not equal to 1/2! Thank you for the help, especially the part about how I needed to separate the conditions for each player to win I don't think I would have figured that part out by myself.

Tags for this Thread

What are tags?

A tag is like a keyword or category label associated with each thread. Tags make it easier for you to find threads of interest.

Anyone can tag a thread. Tags are public and visible to everyone.

Contact us