title: MATLAB Two Dice Thrown
body: <p>I am asked to simulate the sum of 10000 dice rolls (Part A). </p>
  
  <p>My problem is that variable <code>i</code> in the <code>for</code> loop keeps resetting to the number of games played rather than increasing. (so if my <code>i=2</code> and I throw dice 5 times (and the game ends) I want <code>i</code> to be <code>7</code>, but my <code>i</code> would go back to <code>3</code>.) </p>
  
  <p><a href="https://i.stack.imgur.com/knrA7.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/knrA7.png" alt="Problem"></a></p>
  
  <p><strong>Code for Part A</strong></p>
  
  <pre><code>firstwins=0;
   wins=0;
   lost=0;
   for i=1:10000
      reset =0; %reset
      roll =1; %number of rolls in a game
      chck=zeros(12); %array of zeros
      while reset==0 %game is not over yet
           a=randi(6); %roll a die
           b=randi(6); %roll a die
           c=a+b; %two die tossed
          if roll==1 %if this is the first roll of the game
              if c==7 || c==11 %if the sum equals 7 or 11
                  wins=wins+1; %player wins so increment win
                   firstwins=firstwins+1; %#of times won by rolling only once
                   reset=1; %reset
              elseif c==2 || c==3 || c==12 %if the sum is either 2,3, or 12
                   lost=lost+1; %player loses
                   reset=1; %reset
              else %if the sum is neither 2,3,7,11, nor 12
                   roll=roll+1; %increment #of times die was tossed in a game
                   chck(c)=1; %store the sum
              end;
          else %if this is a reroll
              if c==7 %if the rerolled sum == 7
                  lost=lost+1; %player loses
                  reset=1; %reset
              elseif chck(c)==1 %if initial outcome occurred
                  wins=wins+1; %player wins
                  reset=1; %reset
              else %neither 7 or the initial outcome
                  roll=roll+1; %increment the number of rolls in one game
              end;
          end;
      end;  
   end;
   prob=firstwins/10000;
  </code></pre>
  
  <p>Can anyone provide any guidance on Part C?</p>