->Homepage
->Schedule
->Courses
-->CS182
-->Tentative Schedule
-->Objectives
-->Quiz 1
-->Lab 1
-->Lab 2
-->Lab 3
-->Lab 4
-->Lab 5
-->Lab 6
-->Lab 7
-->Project
-->CS183
-->CS285
->Course Policies
->Electronic Submission
->Documentation Standards
->Old Exams
->C++ Examples
->MSVC++ Info
->Software
->Support Forum
->Unix Info
->Nature Photos

[Home]
[Rich][Home][Rich]
[Author]
CS182 -- Lab 7: Estimate PI

Winter 2003-2004

--
Overview

In this lab, you should further develop skills using functions.

Procedures

Write a program that will estimate pi by generating pairs of random numbers between zero and one. You may find it useful to use the equation for the area of a circle when making your estimate.

You should estimate pi using 1) the random number generator found in the cstdlib library and 2) the random number generator described below.

Power Residue Pseudorandom Number Generator

The power residue method is an alternative way of generating pseudorandom numbers. In order to generate pseudorandom integer numbers between 0 and 10n, we begin with an n-digit integer called the seed. In order to get a seemingly random sequence, the seed should be at least four digits long and not be divisible by 2 or 5. You may wish to select this number using the time() function in a way that is similar to setting the seed for the rand() function. The seed is then multiplied by (10n/2 - 3). The lowest n digits of the result is the pseudorandom number generated and is used as the seed for the next pseudorandom number in the sequence.

For example, if we begin with a seed of 234567, we then multiply by (103 - 3) = 997 to get 233863299. The residue of this number is 863299, the second pseudorandom number generated. This is then multiplied by 997 to get 860709103. Therefore, 709103 is the next number generated.

In order to successfully complete the laboratory assignment, you will need to write a function that implements the power residue pseudorandom number generator algorithm. Your function should be a direct replacement for rand with the exception that you should seed it the first time it is called instead of calling an srand look-alike. Note, your random number generator should not make use of rand from the cstdlib library.

Just for Fun

For an extra challenge, you may wish to produce an output file that will allow you to easily import into Excel and plot the points generated by your program.

Lab report (due 11:00pm, the day prior to week 8 lab)

Here is a template file to use as a starting point for this report.

The lab report does not need to be self-contained. Your report should include:

  • A narrative that convinces the reader that you were thinking when doing this assignment. The narrative should include:
    • A description of how your program attempts to estimate pi.
    • A comparison between estimates of pi generated by the two different pseudorandom number generators, including an assessment of which method appears to be a better pseudorandom number generator.
    • Discussion (sample program output, problems you encountered (and how they were overcome), etc.)
    • Any other interesting observations about your experience with this assignment.
  • An activity log indicating how much time you spent on each phase of the assignment. You should use the format found in the template XML file and report the time in the following categories:
    • Design
    • Coding
    • Debug (before you think it's working)
    • Test (after you think it's working)
    • Writing Report
    • Other
  • Documented source code for your program.

As with any report you submit, correct spelling and grammar are required. In addition, your report should be submitted electronically following the Electronic submission guidelines. (You may wish to consult the XML help video and/or sample report before submitting your report.) Be sure to keep copies of all your files, in case something gets lost. It may be wise to keep a diskette backup as well.

Your grade will depend on quality of design, clarity of code and documentation, as well as whether your program produces the correct results. If you have any questions, consult your instructor.

Acknowledgment

This laboratory assignment was developed by Dr. Chris Taylor.

Last Updated: Thursday, 01-Jan-2015 14:33:33 EST