Introduction to Computer Science:
Assignment 19:  Random Numbers and Monte Carlo Methods
  Sewickley Academy, 2000-2001

See Course Home Page.
 
Date Assigned: Fri Nov-3
Date Due: Mon Nov-6

0.  Reminder:  YOU MUST WORK ALONE ON THIS ASSIGNMENT.  You may not work with anyone else on this assignment.

1.  This assignment requires you to use pseudorandom number generators.  C++ has one such function:  rand().  It takes no arguments, and returns an integer in the range [0,RAND_MAX].  Here, we create a function which we'll name rand1(), which takes no arguments and returns a double in the range [0,1].  We did this in class, and the code is included here for you to review (so there is nothing for you to write for this step, just study the code provided):

#include <stdlib.h>  // must include for rand()

// This function returns a floating point number
// in the range [0,1].  It does so by using the
// built-in function rand().  Recall that to use
// rand(), you must include <stdlib.h>
double rand1()
{
 double result;
 result = double(rand()) / RAND_MAX;
 return result;
}

2.  It turns out that we need more functions, though, to do most interesting things with random numbers.  Here we will review the definition of  a function we defined in class which returns a double in the range [lo,hi].  This is useful, for example, when we call findPi(), which needs numbers in the range [-1.0,1.0].    We did this in class, and the code is included here for you to review (so there is nothing for you to write for this step, just study the code provided):
// This function returns a floating point number
// in the range [lo,hi].  It does so by using rand1(),
// multiplying by range, to get a number in [0,hi-lo],
// then adding lo to get a number in [lo,hi].
double random(double lo, double hi)
{
 double result, range;
 range = hi - lo;
 result = lo + rand1() * range;
 return result;
}
3.  In class, we discussed how we can approximate pi using random numbers.  Your task here is to duplicate that effort.  To do this, fill in the missing code below:
double findPi(int tosses)
{
 // Put your code here.  This function should
 // approximate pi by tossing <tosses> darts
 // at the square that encloses the unit circle.
 // That is, at points (x,y) where x and y are
 // both in [-1.0, 1.0] (thus, this square has
 // sides of length 2.0).
 // Now, count how many of these points fall
 // within the unit circle, and call this
 // tossesInUnitCircle.
 // As we discussed in class, pi will be
 // approximately equal to 4.0 times
 // tossesInUnitCircle/tosses.
}

void main()
{
 int tosses;
 double pi;

 cout << "Enter # of tosses: ";
 cin >> tosses;

 pi = findPi(tosses);

 cout << "pi = " << pi << endl;
 getchar();
}

4.  Note that when you run the above program, every time you give it the same number of tosses, it produces the same approximate value of pi.  This is because we need to seed the random number generator.  It is best to do so using the current time, which is different with every run of your program.  To do so, modify your main as follows (no code for you to write here, just study the bold portions of the following code):
#include <time.h>  // for time()

void main()
{
 int tosses;
 double pi;

 // seed the random # generator with the time
 srand(time(NULL));

 cout << "Enter # of tosses: ";
 cin >> tosses;

 pi = findPi(tosses);

 cout << "pi = " << pi << endl;
 getchar();
}

5.  Thus far, only step 3 includes programming, and then it is code which we already wrote in class.  This step is the first one which will make you think a bit.  You are to write a function called coinFlip() which returns 0 if the flip is heads and 1 if the flip is tails.  Use the following code fragment:
const int HEADS = 0;
const int TAILS = 1;

int coinFlip()
{
    // This function simulates a coin flip,
    // returning HEADS or TAILS accordingly.
    // Though it may work any way you wish,
    // you might consider using rand1() here.
    // Just be sure that this function is random
    // and returns HEADS half the time, and TAILS
    // half the time.
}

6.  Now you must add to your main() from above so that it uses this coinFlip() function to have the following behaviors:
Enter number of coinFlips: 100
After 100 coin flips, there were 47 heads and 53 tails.

Enter number of consecutive heads to search for: 3
It took 9 coin flips before there were 3 consecutive heads.

Goodbye!

Note that there are two problems here:  first, you must flip the coin some number of times and report how many heads and how many tails were observed.  Next, you must search for some number of consecutive heads.  In the example above, you would keep flipping coins until you observed HEADS, HEADS, HEADS (3 consecutive), which in the example took 9 coin flips.

Good luck, and have a splendid weekend.

See Course Home Page.