

public class DrunkardsWalk {

   public static void main(String[] args) {

   } // end main()

  
   static void doOneExperiment(int numberOfSteps) {
         // Does one experiment, consisting of 1000 DrunkardWalks.
         // In each walk, the Drunkard takes the specified number
         // of steps.  Data about the experiment are reported by
         // calling the reportData() subroutine.
   } // end doOneExperiment()


   static int doOneWalk(int numberOfSteps) {
         // Simulates a drunkards walk along a line for the specified
         // number of steps.  The drunkard starts at position zero.
         // On each step, the drunkard either takes a step forward,
         // or takes a step backward, or stays in the same place,
         // at random.  The net distance traveled by the drunkard is
         // returned as the value of the function.
      int position;  // The position of the drunkard along a line.
      position = 0;  // The drunkard starts at position zero.
      for (int step = 0; step < numberOfSteps; step++) {
           // Take one step in a random direction, forward or backwards,
           // or stay in the same place.  Each action is equally likely.
         int randomNum = (int)(3*Math.random());  // randomNum is 0, 1, or 2
         if ( randomNum == 0 )
            position++;
         else if ( randomNum == 1 )
            position--;
         // Note that if randomNum is 2, the value of position is unchanged
      }
      return Math.abs(position);  // return the final DISTANCE of the drunkard from 0
   } // end doOneWalk()


   static void printHeaders() {
         // Print out column headers for the output.
      TextIO.putln();
      TextIO.putln("Length of     Average Distance       Square Root of the      Column 3 Divided");
      TextIO.putln("  Walk             Traveled           Average Distance         by Column 1");
      TextIO.putln("----------  ----------------------  ----------------------  ----------------------");
   } // end printHeaders()


   static void reportData(int lengthOfWalk, int numberOfWalks, int sumOfDistances) {
         // Report the data for walks of the specified length.  The length of
         // of the walks is output, along with the average distance of the
         // drunkard at the end of the walk.  The numberOfWalks tells how many
         // times the experiment was repeated.  The sumOfDistances must be
         // the sum of all the distances traveled by the drunkard in the
         // differenet experiments.  Also output is the square root of the
         // average distance, and the square root divided by the length of
         // the walk.  Theoretially, the last column of output should be
         // approximately constant.
      double averageDistance = (double)sumOfDistances / (double)numberOfWalks;
      double sqrtOfDistance = Math.sqrt(averageDistance);
      double diffusionConstant = sqrtOfDistance / lengthOfWalk;
      TextIO.put(lengthOfWalk,6);
      TextIO.put(averageDistance,24);
      TextIO.put(sqrtOfDistance,24);
      TextIO.put(diffusionConstant,24);
      TextIO.putln();
   } // end reportData()


} // end class DrunkardsWalk