CS 1005, C Term 2000
Introduction to Programming
HW3 (due Feb. 18)

This page is located at http://www.cs.wpi.edu/~alvarez/CS1005/HW3/


In this assignment you will use the technique called Monte Carlo simulation to answer a question about n-dimensional space. You will need to use the vector class discussed in the lectures as well as the RandGen class for random number generation described in appendix G.2.4 of the book by Astrachan.

Monte Carlo Simulation

This is a technique that uses randomness to answer questions (not necessarily about random things) that may be difficult to answer in other ways. The particular application that you'll be working with is that of estimating volumes in an n-dimensional space. The idea behind the technique is simple. Think of the 2-dimensional case. Imagine that part of a wall is painted red. The shape of the red part can be very complicated, so it's hard to tell how much of the wall is actually painted red just by guessing. You'd like to know fairly precisely, as a number between 0 and 1, what fraction of the wall's surface is painted red. The Monte Carlo approach is to repeatedly throw a dart at the wall randomly, without aiming (while you're blindfolded, for example). You can tell if the dart hits the red part just by checking. You keep track of how many times you've thrown the dart and of how many times you've hit the red part. The Monte Carlo estimate of the fraction of the wall's surface that is painted red is just the quotient of the number of hits divided by the number of throws.

n-dimensional space

A point in n dimensions can be thought of as a vector containing n real numbers called the coordinates of the point. The distance from a point to the "origin" is the square root of the sum of the squares of the coordinates of the point. The sphere of radius r centered at the origin contains all points whose distance to the origin is less than r. How "snugly" does an n-dimensional sphere fit inside an n-dimensional "cube"? That is the question that you will attempt to answer through Monte Carlo simulation. Rather than throwing darts, you will use a random number generator to pick points inside the cube. Such points have coordinates that range between -1 and +1 (we're considering the sphere of radius 1), but because of geometric symmetry it's ok to just pick points with coordinates between 0 and +1. The fraction of the cube occupied by the sphere can be estimated accurately by looking only at such points. For example, think of the 2D case of a circle inside a square and you'll see that the fraction of the square's area occupied by the circle (which equals pi/4) is the same as the fraction obtained by looking at just one quadrant (positive "x", positive "y").


  1. Review the material on vectors from recent lectures and from chapter 8 of Astrachan. Also refer to appendix G.2.4 for the header file of the RandGen class. You will only need to use the RandReal() member function of the RandGen class. Remember that program examples that use vectors are also available in the appropriate subdirectories of the directory named /cs/cs1005/ on the CCC Unix network.

  2. Log into your CCC Unix account. Inside your personal CS1005 directory, create a subdirectory named HW3. Keep all files for this assignment in that directory. Files containing an outline of the main source file hw3.cxx for this assignment as well as the specification and implementation files for the RandGen class are available in the /cs/cs1005/samples/hw3/ directory. Copy these files to your personal HW3 directory.

  3. Read the specifications of the functions named RandPoint and InSphere that appear in hw3.cxx. Implement these functions, filling in the function bodies in hw3.cxx. For RandPoint you will need to create an object of the RandGen class and to invoke the RandReal() member function for this object a certain number of times. Place any necessary #include directives at the top of the hw3.cxx file.

  4. Write the main() function in hw3.cxx. The user should be prompted to enter the number of dimensions and the desired number of Monte Carlo iterations. Your program should then perform the basic Monte Carlo step the requested number of times. Each step consists of generating a random point in n-dimensional space, testing the point to see if it's inside the n-dimensional sphere, and updating a counter for the number of "hits" appropriately. Finally, the program should report the volume estimate obtained by dividing the total number of hits by the total number of tries. Be careful to perform any necessary conversions to type double when carrying out the arithmetic. A sample run illustrating the desired behavior of the program may be found here.

  5. Compile and link the files for your assignment using the following command line:
       g++ -o hw3prog hw3.cxx randgen.cpp
    Debug your program as necessary. Test your program for different numbers of dimensions and Monte Carlo iterations. Once you're satisfied that your program is behaving as desired, generate a script file named hw3.script that shows several runs of your program. To do this, just type script hw3.script at the Unix prompt. Then run your program as usual. When you're done, type exit to discontinue the logging process. Include runs for dimensions n=2, n=3, n=4. Notice that for n=2 your program should report a volume fraction close to pi/4 (about .785). What happens to the volume fraction reported by the program as the number of dimensions increases?

  6. The source code for your program in hw3.cxx should be commented in detail. Provide additional documentation for your code in a file named hw3.readme, following the WPI CS documentation standard


Your program will be graded based on syntactical soundness, functionality, style, and documentation (including sample runs), with points awarded as indicated below.


Use turnin to submit the following files (and no other files), fully addressing the items listed above, before 3 pm on Friday Feb. 11, 2000.
  1. hw3.cxx
  2. hw3.readme
  3. hw3.script