title: Simulating a random walk scenario on a 4x4 board
body: <p>The problem I am working on is as follows. Imagine a 4x4 chess board on top of which you place a king. The probability of you placing the king on each of the squares is one of the inputs (not necessarily the same for all the squares). The king has to make n number of moves (n is an input). The goal is to run an 'experiment' to find the probability after n moves.</p>
  
  <p>My friend solved the problem by creating a general formula using linear algebra. As a challenge/verification of his answer I want to make this program in c++.</p>
  
  <p>We both start with making one generalization. That is since the board is symmetrical we only consider if the king is at a side, corner or middle square of the board.</p>
  
  <p>C S S C</p>
  
  <p>S M M S</p>
  
  <p>S M M S</p>
  
  <p>C S S C</p>
  
  <p>Therefore, we input only P(S_{initial}), P(C_{initial}), P(M_{initial}), the number of moves the king makes, and the number of 'experiments' we make.</p>
  
  <p>The code for my program is below. My results disagreed with my friend's, so I checked my code by setting n to 1 and P(S_{initial}), P(C_{initial}) and P(M_{initial}) to 1/2, 1/4, and 1/4, respectively. By conditional probability I know that P(S)=59/120, P(C)=21/160, P(M)=181/481. My computations do not agree, so I am sure there is a bug or error in my code, but I cannot track it down. I appreciate any help.</p>
  
  <p>Also I know my code is scruffy and inefficient so I appreciate comments about how to improve it. Thanks for your help.</p>
  
  <p>EDIT: To input the initial probabilities I input 2, 1 and 1 for S, C and M, respectively.</p>
  
  <p>EDIT2: Turned out that I used "=" instead of the operator "==" in my if statements. Thank you @Christopher Oicles. Everything works now as expected.</p>
  
  <pre><code>#include &lt;iostream&gt;
  #include &lt;string&gt;
  #include &lt;cstdlib&gt;
  #include &lt;cmath&gt;
  #include &lt;stdio.h&gt;
  #include &lt;time.h&gt;
  
  using namespace std;
  
  int S, C, M; //probability 'ratios', i.e. prob of S is S/(S+C+M)
  int n; // number of steps
  int trials;
  
  int initial(int a,int b,int c); // this 'throws the die' to find where to start and returns 0 for side, 1 for corner, 2 for middle
  int step(int a); // this throws the die to make a step and returns the same format
  int trial(); //this makes n number of steps, starting at initial(a,b,c) and returns 0,1,2
  
  double data[]={0,0,0}; // the result of the experiment, each element holds the number of times a trial returned the S,C,M where number of s = data[0], I increment this by one every time I run trial();
  
  int main()
  {
      srand(time(NULL));
  
      for(int i=0;i&lt;3;i++) //reset data array
          data[i]=0;
  
      std::cout &lt;&lt; "Enter probability ratio of S:";
      std::cin &gt;&gt; S;
      std::cout &lt;&lt; "Enter probability ratio of C:";
      std::cin &gt;&gt; C;
      std::cout &lt;&lt; "Enter probability ratio of M:";
      std::cin &gt;&gt; M;
      std::cout &lt;&lt; "Enter number of steps per trial:";
      std::cin &gt;&gt; n;
      std::cout &lt;&lt; "Enter number of trials:";
      std::cin &gt;&gt; trials;
  
      for(int i=0;i&lt;trials;i++)
          data[trial()]++;
  
      for(int i=0;i&lt;3;i++)
          std::cout &lt;&lt; data[i]/trials &lt;&lt; " \n";
  
      for(int i=0;i&lt;3;i++)
          std::cout&lt;&lt;data[i]&lt;&lt;" ";
  
      cout &lt;&lt; "\n \n";
  }
  
  int trial(){
      int current;//  the current place S, C or M or 0,1,2
      int next;
  
      current = initial(S,C,M);
  
      for(int i=0;i&lt;n;i++)
          {next = step(current);
          current = next;}
  
      return current;
  }
  
  int initial(int side,int corner,int middle){
      int t = rand() % (side+corner+middle); //generates a random number from zero to a+b+c-1
  
  
      //now we apply the probability
      if(t &lt; side)
          return 0; //side
      if(t &gt;= side &amp;&amp; t &lt; (side+corner))
          return 1; //corner
      if(t&gt;=(side+corner))
          return 2; //middle
  }
  
  int step(int a){
      int t;
  
      switch(a){
          case 0: //we are on a side
              {t = rand()%5;
              if(t &lt; 2){return 0;} //2/5 chance to go to a side
  
              if(t = 2){return 1;} //1/5 chance to go to a corner
  
              if(t &gt; 2){return 2;} //2/5 chance to go to a middle
  
              break;}
          case 1: //we are on corner
              {t = rand()%3;
              if(t &lt; 2){return 0;} //2/3 chance to go to side
  
              if(t = 2){return 2;} //1/3 chance to go to middle
  
              break;}
          case 2: //we are on middle
              {t = rand()%8;
              if(t &lt; 4){return 0;} //1/2 chance go to side
  
              if(t = 4){return 1;} //1/8 chance of corner
  
              if(t &gt; 4){return 2;} //3/8 chance of middle
  
              break;}
      }
  }
  </code></pre>