DAY06
Thursday, 24 May 2012


Don't forget that Monday is a holiday! Our next class will be Wednesday!

Today is a LAB day.

HOMEWORK #1 is due today. You should submit it to Detelina by email. She will always send you a response that she received it; if you think you sent it in and did not hear back, please speak to her today!

We have a LAB #2 to be done during class time, for credit. When you have completed the lab, please let Detelina know so that she can approve and record your work.


LAB#2: Surprisingly, random numbers are an important tool in scientific computing. C includes a number of built-in random number generators. We will take a random number generator for a test drive, and see how it works.

To keep things simple, I'll supply you with a random number generator, called "fran.c". It will give you a random number (a float) between A and B, as long as you give it an integer SEED that it uses to shuffle up the results. fran is a function, and A, B, and SEED are input to the function. However, because SEED will actually also be output from the function (it gets changed on each call), we need to use a special signal when we input SEED, by putting an AMPERSAND in front of it:

        x = fran ( a, b, &seed );
      

#1: Write a program that calls FRAN 3 times in a row, to produce random numbers between 12.5 and 15.0. Print the values you get. Are the values in the correct interval? Are they different? Did SEED change after each call?

#2: Write a program that calls FRAN 100 times in a row, producing random numbers between 10 and 30, and computing the average. You don't know how to do something 100 times, so just copy this code:

        total = 0.0;
        for ( i = 1; i <= 100; i++ )
        {
           total = total + ?
        }
        average = ?
      
Obviously, you need to replace the question marks by code. Is the average what you would guess it to be?


HOMEWORK #2 (must be turned in by next Thursday): Consider the circle of radius 1 centered at (0,0). It completely fits inside a square also centered at (0,0) and with side lengths of 2. If we pick a random point in the square, then the probability that that point is actually also in the circle is

        p = ( area of circle ) / ( area of square ) = pi / 4.0
      
Now let's suppose we don't know the area of the circle. Suppose we pick 1000 random points in the square, and count the number that are inside the circle.
        p = ( "hits" ) / 1000 = ( area of circle ) / ( area of square )
      
so
        area of circle = ( hits / 1000 ) * area of square.
      
The program "circle_area.c" is a start at trying out this computation. The number of random points is described by the variable n. We use a function called fran to compute the random points (x,y). Try to complete the program, adding in the missing declarations, and so on. If your program is correct, it should be able to produce an estimate that's not too bad. Once your program seems to be working, try changing the value n from 100 to 10,000 to 1,000,000 and write down the first four digits of the resulting estimate.

Turn in: send email to Detelina by Thursday, May 31, including:



Last revised on 23 May 2012.