Wednesday, April 4, 2012

Random Walk: Foundational Codes

Imagine a square grid, and start at one corner of it. On each step, if permissible within the boundaries of the grid, move one unit in a randomly chosen cardinal direction. Repeat until the other extreme corner is reached. How many steps does it take? In this post, we'll deal with the programming codes that simulate this scenario. Java and the equivalent MATLAB codes are presented, with commenting on the Java code.

Java version:
public class RandomWalk
{
 public static void main(String[] args)
 {
     int[] coor = new int[2];    //this houses the (x,y) coordinate
     coor[0] = coor[1] = 1;        //begin at (1,1)
     int move = 0;
     final int size = 10;        //this creates 10x10 grid
   
     while(!(coor[0]==size && coor[1]==size))    //game continues while not at the extreme corner
     {
         boolean valid = false;    //indicates whether or not a valid location has been found to move to
         while(!valid)            //while there isn't a valid location, search for one
         {
             double rand = Math.random();
             int[] test = new int[2];    //use to house the next potential location
             test[0] = coor[0];
             test[1] = coor[1];
           
             if(rand < 0.25)    //corresponds to moving north
                 test[1]++;
             else if(rand < 0.5)    //moving east
                 test[0]++;
             else if(rand < 0.75)    //moving south
                 test[1]--;
             else                    //moving west
                 test[0]--;
           
            //next location is valid if it's within the boundaries of the grid
             if(test[0] >= 1 && test[0] <= size && test[1] >= 1 && test[1] <= size)
             {//if a valid location is found, transfer the coordinate points
                 coor[0] = test[0];
                 coor[1] = test[1];
                 valid = true;
             }
         }//otherwise, repeat the loop to try to find another valid location
         move++;    //increment move if a valid move is made
     }
     //after the end of the game, indicate the total number of moves
     System.out.println("Number of moves: " + move);
 }
}

MATLAB version:
coor = [1,1];
numMoves = 0;
size = 10;

while ~(coor(1) == size && coor(2) == size)
    valid = false;
    while ~valid
        r = rand;
        test = zeros(1,2);
        test(1) = coor(1);
        test(2) = coor(2);
       
        if r < 0.25
            test(2) = test(2) + 1;
        elseif r < 0.5
            test(1) = test(1) + 1;
        elseif r < 0.75
            test(2) = test(2) - 1;
        else
            test(1) = test(1) - 1;
        end
       
        if(test(1) >= 1 && test(1) <= size && test(2) >= 1 && test(2) <= size)
            coor(1) = test(1);
            coor(2) = test(2);
            valid = true;
        end
    end
   
    numMoves = numMoves + 1;
end
numMoves

In subsequent posts, we'll work with this code and add variations.