PHY 411-506 Home Home  |  Course Outline  |  Lectures  |  Homework  |  Files

Lecture 10: February 7


A random walk in two dimensions

The two-dimensional random walk simulation discussed on page 375 is interesting beacuse it uses an ensemble of independent walkers similar to the ensemble we imagined in constructing a thermalization proof for the Metropolis algorithm; it is also interesting because it provides a simple example which illustrates the use of the comphys.graphics package classes to program a simple animation of the walk. The following applet is based on RandomWalk2.java is a straightforward translation of PROGRAM random_walk2:

More on graphics programming

Here is the code which generates the two-dimensional random walk:

// RandomWalk2.java

import comphys.*;
import comphys.graphics.*;

public class RandomWalk2 extends Animation {

    int nwalkers = 100;         // number of walkers
    int N;                      // number of steps
    int[] xWalker, yWalker;     // walkers' current positions

    void initial () {
        if (xWalker == null || xWalker.length < nwalkers) {
            xWalker = new int[nwalkers];
            yWalker = new int[nwalkers];
        }
        for (int n = 0; n < nwalkers; n++)
            xWalker[n] = yWalker[n] = 0;
        N = 0;
    }

    void move () {
        for (int n = 0; n < nwalkers; n++)
            choice(n);
        ++N;
    }

    void choice (int n) {
        double p = Math.random();
        if (p <= 0.25) {
            ++xWalker[n];
        } else if (p <= 0.5) {
            --xWalker[n];
        } else if (p <= 0.75) {
            --yWalker[n];
        } else {
            ++yWalker[n];
        }
    }

    double rMax = 20;
    boolean needToClear = true;
    
    class Movie extends Plot {
        public void paint () {
            if (needToClear) {
                clear();
                needToClear = false;
            }
            setColor("gray");
            drawAxes(-rMax, rMax, -rMax, rMax);
            setColor("green");
            for (int n = 0; n < nwalkers; n++)
                boxArea(xWalker[n] - 0.5, xWalker[n] + 0.5,
                        yWalker[n] - 0.5, yWalker[n] + 0.5);
            plotOverlay();
            setPaintMode();
            setColor("red");
            for (int n = 0; n < nwalkers; n++)
                boxArea(xWalker[n] - 0.5, xWalker[n] + 0.5,
                        yWalker[n] - 0.5, yWalker[n] + 0.5);
            setColor("blue");
            plotStringCenter("Step number = " + N, 0, -1.15 * rMax);
        }
    }

    Movie movie;
    Reader nwalkersReader, rMaxReader;

    public void init () {
        initial();
        add(movie = new Movie());
        add(nwalkersReader = new Reader("n walkers = ", nwalkers));
        add(rMaxReader = new Reader("r_max = ", rMax, 4));
        addControlPanel();
    }

    public void step () {
        move();
        movie.repaint();
    }

    public void reset () {
        nwalkers = nwalkersReader.readInt();
        rMax = rMaxReader.readDouble();
        initial();
        needToClear = true;
        movie.repaint();
    }

    public static void main (String[] args) {
        RandomWalk2 rw2 = new RandomWalk2();
        rw2.frame("Random Walk in 2 Dimensions", 500, 600);
    }
}

This program uses two comphys.graphics classes:

Note, finally, that an Animation can be run as an applet in a Web browser or as an application from the command line. When invoked as an applet, the appletviewer or Web browser initializes your program by invoking the public void init () method. To run the program as an application from the command line, you need to define a main method, for example:

    public static void main (String[] args) {
        RandomWalk2 rw2 = new RandomWalk2();
        rw2.frame("Random Walk in 2 Dimensions", 500, 600);
    }
Inside the main method you need to do two things:

  1. First, create an instance of the program public class, and

  2. Create a window on the screen to for your graphical user interface. comphys.graphics.AppNlet.frame method creates for you a frame with the specified title, and the specified width and height in pixels.


UB Physics Home Questions or comments: phygons@acsu.buffalo.edu