CS 1073 Introductory Programming
for Scientific Applications
Random Walk in the Plane


Java Program to carry out a random walk in the plane: The program below shows one implementation.

Random Walk in the Plane

public class RanWalk {
   
   public final int MAXP = 40;
   private char[][] plane = new char[MAXP][MAXP];
   private String alf = "ABCDEFGHIJKLMNOPQRSTUVWXYZ" + 
                "abcdefghijklmnopqrstuvwxyz";
   private int x = MAXP/2, y = MAXP/2; /* current location */
   private int steps = 0; /* number of steps taken */

   public void blankPlane() {
      for(int i = 0; i < MAXP; i++)
         for(int j = 0; j < MAXP; j++)
            plane[i][j] = ' ';
   }

   public void oneStep() {
      if (Math.random() < 0.5) { /* change x */
         if (Math.random() < 0.5) x = x + 1;
         else x = x - 1;
      }
      else { /* change y */
         if (Math.random() < 0.5) y = y + 1;
         else y = y - 1;
      }
   }

   public void printPlane() {
        System.out.println("Number of steps to the boundary: " + 
           steps + "\n");
        System.out.print("   ");
        for(int j = 0; j < MAXP; j++)
            System.out.print(j/10);
        System.out.println("|");
        System.out.print("   ");
        for(int j = 0; j < MAXP; j++)
           System.out.print(j%10);
        System.out.println("|");
        System.out.print("   ");
        for(int j = 0; j < MAXP; j++)
           System.out.print("-");
        System.out.println("|");
        for(int i = 0; i < MAXP; i++) {
           if (i < 10) System.out.print(" " + i + "|");
           else System.out.print(i + "|");
           for(int j = 0; j < MAXP; j++)
              System.out.print(plane[i][j]);
           System.out.println("|");
        }
        System.out.print("   ");
        for(int j = 0; j < MAXP; j++)
                System.out.print("-");
        System.out.println("|");
   }

   public void createRanWalk() {
      while( x >= 0 && x < MAXP && y >= 0 && y < MAXP) {
         steps++;
         plane[x][y] = alf.charAt((steps/20)%52);
         oneStep();
      }
   }
   
   public static void main(String[] args) {
        RanWalk ranW = new RanWalk();
        ranW.blankPlane();
        ranW.createRanWalk();
        ranW.printPlane();
   }
}


Features of this program:

  1. This program presents a random walk in the plane. The program lays out a 40-by-40 square of characters in a 2-dimensional array. It start a path at the center. Then randomly it moves on square up, down, left, or right, each with equal probability.
  2. The program moves from square to square at random until it first reaches the boundary. Then it stops and gives the number of steps it took to get to the boundary.
  3. Each time the random walk reaches a square it puts a non-blank character into that square. Instead of just putting a '*' on the path, the program puts 10 'A's, then 10 B's, ..., then 10 'Z's, then 10 'a's, ..., then 10 'z's, and then it starts over with 'A' again. Thus after 1040 steps, it goes through the upper- and lower-case letters, and then start over.


Here are results of runs.




Here's a run with a 60-by-60 square, instead of 40-by-40.


Random Walk in the Plane

public class RanWalkStat {
   
   public final int MAXP = 60;
   private int x, y; /* current location */
   private int steps; /* number of steps taken */

   public void init() {
      steps = 0;
      x = MAXP/2;
      y = MAXP/2;
   }
   
   public void oneStep() {
      if (Math.random() < 0.5) { /* change x */
         if (Math.random() < 0.5) x = x + 1;
         else x = x - 1;
      }
      else { /* change y */
         if (Math.random() < 0.5) y = y + 1;
         else y = y - 1;
      }
   }
   
   public int runRanWalk() {
      init();
      while( x >= 0 && x < MAXP && y >= 0 && y < MAXP) {
         steps++;
         oneStep();
      }
      return steps;
   }
   public static void main(String[] args) {
        RanWalkStat ranW = new RanWalkStat();
        int[] walkStats = new int[80];
        for (int i = 0; i < 100000; i++) {
           int steps = ranW.runRanWalk();
           if (steps/100 >= walkStats.length) {
              walkStats[walkStats.length -1]++;
              System.out.println(steps);
           }
           else walkStats[steps/100]++;
           // System.out.println(steps);
        
        }
        for (int i = 0; i < walkStats.length; i++)
           System.out.println(i*100 + "-" + ((i+1)*100 - 1) + 
              ": \t"  + walkStats[i]);
   }
}