/** * A class that performs repeated random alley walk simulations (using an * AlleyWalker object) and calculates the average number of steps required * for each walk. * @author Dave Reed * @version 11/7/06 */ public class WalkStats { private int alleyLength; private AlleyWalker walker; /** * Constructs a WalkStats object. * @param distance the goal distance for the RandomWalker */ public WalkStats(int distance) { this.alleyLength = distance; this.walker = new AlleyWalker(); } /** * Gets the length of the alley the walker is in. * @return the alley length (from the middle to either end) */ public int length() { return this.alleyLength; } /** * Simulates a number of random walks and maintains statistics. * @param numWalks the number of walks to be simulated * @return the average number of steps required for the walks */ public double averageSteps(int numWalks) { if (numWalks <= 0) { return 0.0; } else { int totalSteps = 0; for (int walk = 1; walk <= numWalks; walk++) { walker.reset(); walker.stepToDistance(this.length()); totalSteps += walker.getNumSteps(); } return (double)totalSteps/numWalks; } } }