xvi Contents
7.5 E x ampl e: Class for Complex Numbers . . . . . . . . . . . . . . . 343
7.5.1 Implementation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 344
7.5.2 Illegal Operations . . . . . . . . . . . . . . . . . . . . . . . . . . . 345
7.5.3 Mixing Complex and Real Number s . . . . . . . . . . . 346
7.5.4 Special Methods for “Right” Operands . . . . . . . . . 348
7.5.5 Inspecting Instances . . . . . . . . . . . . . . . . . . . . . . . . . 350
7.6 Static Methods and Attributes . . . . . . . . . . . . . . . . . . . . . . 351
7.7 Sum mar y . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 352
7.7.1 Chapter Topics . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 352
7.7.2 Summarizing Example: Interval Arithmetics . . . . 353
7.8 E x er ci ses . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 359
8 Random Numbers and Simple Games . . . . . . . . . . . . 375
8.1 Dr awing Random Numbers . . . . . . . . . . . . . . . . . . . . . . . . . 376
8.1.1 The Seed . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 376
8.1.2 Uniformly Distributed Random Numbers . . . . . . 377
8.1.3 Visualizing the Distribution . . . . . . . . . . . . . . . . . . 378
8.1.4 Vectorized Drawing of Random Numbers . . . . . . 379
8.1.5 Computing the Mean and Standar d Deviation . . 380
8.1.6 The Gaussian or Normal Distr ib ution . . . . . . . . . 381
8.2 Dr awing Integers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 382
8.2.1 Random Integer Functions . . . . . . . . . . . . . . . . . . . 383
8.2.2 Example: Throwing a Die . . . . . . . . . . . . . . . . . . . . 384
8.2.3 Drawing a Random Element from a List . . . . . . . 385
8.2.4 Example: Drawing Cards from a Deck . . . . . . . . . 385
8.2.5 Example: Class Implementation of a De ck . . . . . 387
8.3 Compu ting Probabilities . . . . . . . . . . . . . . . . . . . . . . . . . . . 390
8.3.1 Principles of Monte Carlo Simulation . . . . . . . . . . 390
8.3.2 Example: Throwing Dice. . . . . . . . . . . . . . . . . . . . . 391
8.3.3 Example: Drawing Balls from a Hat . . . . . . . . . . . 393
8.3.4 Example: Policies for Limiting Population
Growth . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 395
8.4 Sim pl e Games . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 398
8.4.1 Guessing a Number . . . . . . . . . . . . . . . . . . . . . . . . . 398
8.4.2 Rolling Two Dice . . . . . . . . . . . . . . . . . . . . . . . . . . . 398
8.5 Monte Carlo Integration . . . . . . . . . . . . . . . . . . . . . . . . . . . 401
8.5.1 Standard Monte Carlo Integration . . . . . . . . . . . . 401
8.5.2 Area Computing by Throwing Random Points . . 404
8.6 Random Walk in One Space Dimension . . . . . . . . . . . . . . 406
8.6.1 Basic Implementation . . . . . . . . . . . . . . . . . . . . . . . 406
8.6.2 Visualization . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 407
8.6.3 Random Walk as a Difference Equation . . . . . . . . 408
8.6.4 Computing Statistics of the Particle Positions . . 408
8.6.5 Vectorized Implementation . . . . . . . . . . . . . . . . . . . 409
8.7 Random Walk in Two Space Dimensions . . . . . . . . . . . . . 411