Random Numbers

For the first part of today, we're going to learn about using rand() and srand() from the cstdlib library to generate random numbers and do stuff with them. First let's go over the basics.

- Use rand() to create a random number between 0 and RAND_MAX
- Set the seed using srand(NUMBER_HERE) to determine where your
   numbers are drawn from.
   - Each time you use srand() the generator will restart from
     the beginning. For most applications we use srand just once
     at the beginning of the code.
        (usually don't want to repeat the same numbers)

We usually want more control over the numbers we create. For example, instead of creating numbers between 0 and RAND_MAX, how about numbers between 0 and 3? Here are some formulas. We'll talk about them a little in class, but it is OK to just memorize them and not worry about the math.

  To create a random integer between a and b (inclusive), use:
       rand()%(b-a+1) + a

  To create a random double between a and b (inclusive), use:
       (double)rand()/RAND_MAX * (b-a) + a

Here's a short .cpp file with some examples


Finding probabilities via simulation

Next, we're going to work on a bigger problem together. Here's a probability problem: If I flip 3 coins, what's the chance that I get heads on all 3? What about on 2 out of the 3?

If you've taken a probability class before, you can probably solve this in your head, but we're going to show how to solve this using a computer simulation. People call these types of solution "Monte-Carlo Methods". This kind of idea is very useful when dealing with harder problems that we can't solve with pencil and paper.

As always when solving longer problems, we're going to put our solution together step by step, testing along the way. Like last week, we're going to build this together in class. But if you're looking at this later, here's a rough idea of how we went through it:

    // Flip 3 coins, output on the screen if we get heads or tails.
    // Count the total number of heads and output that as well

    // Repeat the process in coinFlip1 many many times.
    // Each time keep track of the number of heads.
    // After many trials we should have a rough idea of the probability of
    // each outcome.

Calculating pi via simulation

The background idea behind this problem is similar to the coin flip problem; we can use random numbers and simulation to come up with approximate solutions to problems. In this example we're going to calculate pi. Here's the idea: take the circle with radius 1 (centered at (0,0)), and put it inside the square of side-length 2 (also centered at (0,0)).

If I pick a random point inside the square, there's a chance that it will land in the circle (the red points above), and a chance that it will not (the blue points). In fact, we know that

    Prob( Point lands in circle ) = (Area of circle) / (Area of square)

The area of the square is 4, and the area of the circle is pi, so after some algebra we get

    pi = 4 * Prob( Point lands in circle)

So we're going to write some code to create random points in the square, and see how many of them land in the circle. Once we multiply by 4, we get an approximation of pi! As before, we'll write this together in class, but here's a finished version: