DOC PREVIEW
Yale CPSC 427 - Object-Oriented Programming
School name Yale University
Pages 14

This preview shows page 1-2-3-4-5 out of 14 pages.

Save
View full document
View full document
Premium Document
Do you want full access? Go Premium and unlock all 14 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 14 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 14 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 14 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 14 pages.
Access to all documents
Download any document
Ad free experience
Premium Document
Do you want full access? Go Premium and unlock all 14 pages.
Access to all documents
Download any document
Ad free experience

Unformatted text preview:

OutlineRemarks on Problem SetsProblem Set 1Problem Set 2BarGraph Demograph.hppOutline Problem Sets BarGraph DemoCPSC 427a: Object-Oriented ProgrammingMichael J. FischerLecture 7September 23, 2010CPSC 427a 1/14Outline Problem Sets BarGraph DemoRemarks on Problem SetsProblem Set 1Problem Set 2BarGraph Demograph.hppCPSC 427a 2/14Outline Problem Sets BarGraph DemoRemarks on Problem SetsCPSC 427a 3/14Outline Problem Sets BarGraph DemoProblem Set 1Problem Set 1Seth HammanCPSC 427a 4/14Outline Problem Sets BarGraph DemoProblem Set 2Problem Set 2Random number generation and simulationsCPSC 427a 5/14Outline Problem Sets BarGraph DemoProblem Set 2Pseudorandom number generatorsYou will need to generate random numbers in this assignment.A few remarks on random number generation are in order.IPseudorandom numbers are not random. They arepredictable. This is both an asset and a curse.ISince they are predictable, a simulation run can be repeated toobtain the same results, particularly helpful during debugging.ISince they are not random, they may have statisticalproperties that differ from true random numbers.I“Good” pseudorandom numbers should pass commonstatistical tests for randomness.CPSC 427a 6/14Outline Problem Sets BarGraph DemoProblem Set 2Random numbers in C++Irand() is standard random number generator in C and C++.Irand() implementation on current Linux systems is good butnot on some other systems.INewer and better random number generators might bepreferable for real-world applications.CPSC 427a 7/14Outline Problem Sets BarGraph DemoProblem Set 2rand() and srand()Basic propertiesIint rand(void) generates next number in sequence usinghidden internal state.INot thread safe.Ivoid srand(unsigned int seed) initializes the state.ISeed defaults to 1 if srand() not called.Irand() returns an int in the range [0...RAND MAX].IMust #include <cstdlib>IRAND MAX is typically the largest positive number that can berepresented by an int, e.g., 231− 1.IThe result from rand() is rarely useful without furtherprocessing.CPSC 427a 8/14Outline Problem Sets BarGraph DemoProblem Set 2Generating uniform distribution over a discrete intervalTo generate a uniformly distributed number u ∈ {0, 1, . . . , n − 1}:INaive way: u = rand()%n.Problem: Result not uniformly distributed unless n |RAND MAX.IBetter way: See problem set.int RandomUniform(int n) {int top = ((((RAND_MAX - n) + 1) / n) * n - 1) + n;int r;do {r = rand();} while (r > top);return (r % n);}CPSC 427a 9/14Outline Problem Sets BarGraph DemoProblem Set 2Generating random doublesTo generate a double in the semi-open interval [0 . . . 1):(double) rand() / ( (double)(RAND MAX) + 1.0 )IWithout + 1.0, result is in the closed interval [0 . . . 1].I(double) rand() / ( RAND MAX + 1 )might fail because of integer overflow.CPSC 427a 10/14Outline Problem Sets BarGraph DemoProblem Set 2Alternate method for generating uniform distribution overa discrete intervalTo generate a uniformly distributed number u ∈ {0, 1, . . . , n − 1}:1. #include <cmath>.2. Generate a uniformly distributed random double u in [0 . . . 1).3. Compute trunc(n*u).Question: Is this truly uniform over {0, 1, . . . , n − 1}?CPSC 427a 11/14Outline Problem Sets BarGraph DemoProblem Set 2Generating exponential distribution[Not needed for PS2 but useful to know.]To generate a double according to the exponential distributionwith parameter lambda:1. #include <cmath>.2. Generate a uniformly distributed random double u in [0 . . . 1).3. Compute -log(1.0-u)/lambda.Note: log(0.0) is undefined. Will return a special value thatprints as -inf.CPSC 427a 12/14Outline Problem Sets BarGraph DemoBar Graph DemoWe look at the Bar Graph demo fromChapter 8 of the textbook.CPSC 427a 13/14Outline Problem Sets BarGraph Demograph.hppclass Graph {private:Row* bar[BARS]; // List of bars (aggregation)void insert( char* name, int score );public:Graph ( istream& infile );~Graph();ostream& print ( ostream& out );// Static functions are called without a class instancestatic void instructions() {cout << "Put input files in same directory ""as the executable code.\n";}};inline ostream& operator<<( ostream& out, Graph& G) {return G.print( out );}CPSC 427a


View Full Document

Yale CPSC 427 - Object-Oriented Programming

Download Object-Oriented Programming
Our administrator received your request to download this document. We will send you the file to your email shortly.
Loading Unlocking...
Login

Join to view Object-Oriented Programming and access 3M+ class-specific study document.

or
We will never post anything without your permission.
Don't have an account?
Sign Up

Join to view Object-Oriented Programming 2 2 and access 3M+ class-specific study document.

or

By creating an account you agree to our Privacy Policy and Terms Of Use

Already a member?