title: How can I find the average dead-end length and escape length of a self avoiding walk?
body: <p>I'm building a program that takes command-line arguments n and trials and computes trials self-avoiding walks in an n-by-n lattice. For each walk, it creates a boolean array, starts the walk in the center, and continues until either a dead end or a boundary is reached. The result of the computation is the percentage of dead ends. Increasing the number of experiments increases the precision.</p>
  <p>I have been able to simulate the self-avoiding walk and calculate the average length of the walk, but I am trying to find the average dead-end length and escape length of the simulation. I have tried to find the dead-end length by first using this statement within the dead-end if statement:</p>
  <pre><code>deadEndLength += length;
  </code></pre>
  <p>After the if statement:</p>
  <pre><code>System.out.println(&quot;Average dead-end length&quot; + deadEndLength/deadEnds);
  </code></pre>
  <p>This statement results in a length that is incorrect and too large.  Now, I am trying to make deadEndLength an array that is currently present in the code below.</p>
  <p>I have also tried implementing Math.abs() for the x and y values to calculate the length, but it doesn't seem to help me find a solution.</p>
  <p>n: lattice size</p>
  <p>trials: number of trials</p>
  <p>deadEnds: number of trials resulting in a dead-end</p>
  <p>escapes: number of trials resulting in an escape</p>
  <p>length: length of all trials in terms of distance</p>
  <p>deadEndLength: length of trial resulting in a dead-end</p>
  <p>escapeLength: length of trial resulting in an escape</p>
  <p>a[][]: intersections visited</p>
  <p>x, y: current position</p>
  <p>r: random number in (0, 1)</p>
  <pre><code>public class SelfAvoidingWalkMod 
  {
      public static void main(String[] args)
      {
          int n = Integer.parseInt(args[0]);
          int trials = Integer.parseInt(args[1]);
          int deadEnds = 0;
          int escapes = 0;
          int length = 0;
          int escapeLength = 0; 
          double[] deadEndLengths = new double[trials];
          for (int t = 0; t &lt; trials; t++)
          {   // Do trials random self-avoiding walks in an n-by-n lattice.
              boolean[][] a = new boolean[n][n];
              int x = n/2;
              int y = n/2;
              while (x &gt; 0 &amp;&amp; x &lt; n - 1 &amp;&amp; y &gt; 0 &amp;&amp; y &lt; n - 1)
              {   // Check for dead end and make a random move.
                  a[x][y] = true;
                  if (a[x-1][y] &amp;&amp; a[x+1][y] &amp;&amp; a[x][y-1] &amp;&amp; a[x][y+1])
                  {
                      deadEnds++;
                      for (int i = deadEnds; i &lt; deadEnds+1; i++)
                      {
                          deadEndLengths[i] = length;
                      }
                      break;
                  }
                  double r = Math.random();
                  if      (r &lt; 0.25) { if (!a[x+1][y]) x++; length++; } // If the value of random is less than 0.25, then if the coordinate at a[x+1][y] is false, add 1 to the x coordinate and add one to length
                  else if (r &lt; 0.50) { if (!a[x-1][y]) x--; length++; }
                  else if (r &lt; 0.75) { if (!a[x][y+1]) y++; length++; }
                  else if (r &lt; 1.00) { if (!a[x][y-1]) y--; length++; }
              }
          }
          escapes = trials - deadEnds;
          System.out.println(100 * deadEnds/trials + &quot;% dead ends&quot;);
          System.out.println(&quot;Dead-ends: &quot; + deadEnds);
          System.out.println(&quot;Length: &quot; + length);
          System.out.println(&quot;Average length: &quot; + length/trials);
          System.out.println(&quot;Average dead-end length: &quot;);
          System.out.println(&quot;Average escape length: &quot;);
      }    
  }
  </code></pre>
  <p>EDIT:</p>
  <pre><code>    for (int t = 0; t &lt; trials; t++)
      {   // Do trials random self-avoiding walks in an n-by-n lattice.
          boolean[][] a = new boolean[n][n];
          int x = n/2;
          int y = n/2;
          length = 0;
          while (x &gt; 0 &amp;&amp; x &lt; n - 1 &amp;&amp; y &gt; 0 &amp;&amp; y &lt; n - 1)
          {   // Check for dead end and make a random move.
              a[x][y] = true;
              if (a[x-1][y] &amp;&amp; a[x+1][y] &amp;&amp; a[x][y-1] &amp;&amp; a[x][y+1])
              {
                  deadEnds++;
                  deadEndLength += length;
                  break;
              }
              double r = Math.random();
              if      (r &lt; 0.25) { if (!a[x+1][y]) x++; length++; } // If the value of random is less than 0.25, then if the coordinate at a[x+1][y] is false, add 1 to the x coordinate and add one to length
              else if (r &lt; 0.50) { if (!a[x-1][y]) x--; length++; }
              else if (r &lt; 0.75) { if (!a[x][y+1]) y++; length++; }
              else if (r &lt; 1.00) { if (!a[x][y-1]) y--; length++; }
          }
          totalLength += length;
      }
      escapes = trials - deadEnds;
      escapeLength = totalLength - deadEndLength;
      System.out.println(100 * deadEnds/trials + &quot;% dead ends&quot;);
      System.out.println(&quot;Dead-ends: &quot; + deadEnds);
      System.out.println(&quot;Total length: &quot; + totalLength);
      System.out.println(&quot;Average length: &quot; + totalLength/trials);
      System.out.println(&quot;Total dead-end length: &quot; + deadEndLength);
      System.out.println(&quot;Average dead-end length: &quot; + deadEndLength/deadEnds);
      System.out.println(&quot;Total escape length: &quot; + escapeLength);
      System.out.println(&quot;Average escape length: &quot; + escapeLength/escapes);
  }    
  </code></pre>
  <p>}</p>