CPS 100E, Fall 1996 Lab 2

Random Walks, Observer Classes, News and Email

(You may find it easier to read this lab using Netscape or another browser, the URL is http://www.cs.duke.edu/~ola/courses/cps100e/lab/lab2.html)

The goals of this lab include doing the specific tasks outlined below and understanding the general concepts behind the tasks.


In this lab we will look at using some classes to simulate random walks. Ideally you would have read chapter 7 of Astrachan, but because of the late arrival of the books and because we didn't cover the RandomWalk class in during class-time, some explanation is in order. Quoting Astrachan,

``A random walk is a model built on mathematical and physical concepts that is used to explain how molecules move in an enclosed space. This model is also the basis for several models that predict stock market prices''.

In one dimension, the idea would be you flip a coin. If the coin comes up heads, you take a step to your left, if it comes up tails, you take a step to the right. You continue doing this for some period of time, and you see how many steps away from your starting position, you end up. In two dimensions, you could roll a four-sided die and based on the die's outcome, you could take a step north, south, east, or west.

Lab 2 table of contents

[ Basic Walks | Animation | Modifications | Submit | Reading News ]

Compiling and Executing/Using the Animator Samba

In this section of the lab you'll copy files, compile a basic simulation of a random walk, and view the simulation using an animation tool.

First change into your cps100e subdirectory (type pwd to verify where you are). Create a lab2 subdirectory by typing mkdir lab2 and change into this subdirectory (be sure to check that you're in the lab2 subdirectory.) Now copy the files for the lab (don't forget the . when copying).

cp ~ola/cps100e/lab2/* .

You should see the files listed below (these are links to the files in case you use Netscape, and for users outside of Duke).

Be sure you're in the lab2 subdirectory, and check to see that all files are there (type ls). Then, from an xterm window (at the prompt [1] ola@teer8% or similar) compile the first version of the program by typing: make frogs . This should compile two files and link them together with the library libtapestry.a. Now run the program by typing: frogs at the prompt. The program will prompt you for the number of steps you wish to take in a random walk. Although it may seem to do nothing, it is creating a file to store information about the random walk that was simulated. If you type ls after the program runs, you should see a file called frog.out which was created by frogs. Type ls -lt and write down here _______________ how big the file frog.out is. (the -lt means long format, and sorted by time.)

Animated Walks

To see an animated re-play of the walk you will run an animator called samba using the file you created when the walk simulation was run. To run the animator type: samba frog.out

( If you get a message samba: command not found, then you didn't change your path in lab1 and you should check with a TA. )

To make samba work, you'll need to click on the start button in the Polka Control Panel. You can slow the animation down using the slide-bar. You can pause/start using the pause/start button. To quit samba, use the quit button.

back to lab contents


Modifications to Frogs: One-D to Two-D

In this part of the lab you'll change the one-dimensional walker to a two-dimensional walker. First you'll modify the program frogs to use four frogs instead of two.

More Frogs

  1. Run emacs, either by clicking on the emacs icon in the menubar (if there is one) or typing emacs & at a prompt.

  2. Load (use the file menu or C-x C-f) the file frogs.cc.

  3. Change the number of walkers/frogs from 2 to 4 (this is a constant at the top of the program). Define two new variables, initialized in the same way that frog and toad are initialized.

  4. Inside the loop, call the Next() member function for each of the newly defined random-walking frogs.

  5. Recompile the program: C-x c, or M-x compile, followed by make frogs in the minibuffer. If you have errors, use C-x ` (that's a back-quote) to have emacs find the error(s) --- correct them one at a time.

  6. When the program compiles, run it, then run samba again on the output file by typing samba frog.out

More Dimensions

For this part of lab, you'll change the one-dimensional east/west frog to a two-dimensional east/west/north/south frog. To do this you'll change the private section of the class RandomWalk which will require changing member functions as well (and these changes will require still more changes.)

  1. Load walk.h into your emacs editor.

  2. Now load README into the editor (it will be blank since it doesn't exist yet). Switch emacs buffers back to walk.h by either using the mouse and the Buffers menu, or typing C-x b and the name of the buffer, walk.h, at the minibuffer prompt.

  3. In the private section is a data variable myPosition that is used to keep track of a walker's location. You'll need both an X and a Y position for a two-dimensional walker. To do this replace myPosition with two private data members: myXposition and myYposition.

  4. You'll need to modify the constructor function RandomWalk() to initialize both myXposition and myYposition to 0.

  5. You can try to think of what other changes will be needed --- this is a good idea in general. You can also get the compiler to help find errors. First, some fixes you need to make as a result of changing to a two-dimensional walk are outlined here:
    • Modify the TakeStep() member function so that a 4-sided die is rolled. If the roll comes up 1 go west by decreasing the value of myXposition, if the roll is 2 go east by increasing the value of myXposition (this is similar to what happens with the on-dimensional frog.)

      If the roll is 3 or 4 change myYposition by subtracting/adding one, respectively.

    • Modify the First() member function in walk.cc to set both myXPosition and myYPosition to 0.

    • Modify the GetPosition() member function so that it can return both X and Y coordinates. To do this you'll need to use reference parameters which we haven't talked about in class yet. Here's the new member function. void RandomWalk::GetPosition(int & x, int & y) { x = myXposition; y = myYposition; } Make this change in walk.cc, you'll also need to change the prototype in walk.h to void GetPosition(int & x, int & y);

  6. When you've made these changes, re-compile frogs by typing C-x c then make frogs.

    You should notice some errors. Cut and paste the errors using the mouse or using control commands that the TAs will help with. Cut the errors and paste them into your README file.

  7. Fix the errors, you're on your own for errors in walk.cc and in walk.h which are described in the steps above. If there is an error in observer.cc have emacs put you at the line with the error by typing C-x `.

    You'll see that GetPosition() is called incorrectly. The x and y coordinate should be arguments passed to GetPosition() [you won't need to call GetYFromID() any more].

  8. Recompile the program, hopefully it will compile successfully. If not, try to track down the errors (ask for help!).

  9. When the program compiles, you'll still need to make one more change for the animation to be "right". In the member functin InitializeStream() in observer.cc you'll need to change double yStart = GetYFromID(k);

    to

    double yStart = 0.0

    Because all frogs now start in the middle of the screen. Enter this change, the re-compile the program.

  10. Once the program is recompiled, re-run it, then run samba on the output file. Write in your README file a few sentences that describe what the animation looks like.

back to lab contents


Submitting The Lab

To submit assignments you'll run the command below, but substitute your section number (1, 2, or 3) for N.
    submit100e lab2.N README frog.cc walk.cc walk.h observer.cc

You can enter the files in any order (you don't need to submit observer.h because it didn't change, but you can submit it.)

Remember that every assignment must have a README file submitted with it (please use all capital letters). Include your name, the date, and an estimate of how long you worked on the assignment in the README file. You must also include a list of names of all those people with whom you collaborated on the assignment.

back to lab contents


Reading News/Sending email

You should post a news message (there are several ways to post news, you can use pine, probably netscape, and a program called Pnews.) Ask the TAs about this if you don't already now. You must post news to the newsgroup duke.cs.cps100e with the subect line personal bio.

Your post should include a description of who you are, where you're from, what your interests are, and other similar information. You should also include whether you own a computer and what language you've programmed in before. Be sure to use the right subject line as described above.

You should also send email to the head UTA Rachel Pottinger: rap@cs.duke.edu with the subject line: lab2 email. You should get an acknowledgment back, but not a lengthy reply.