91.166 Copyright © 2000, Department of Systems and Computer Engineering, Carleton University 1 A Simulation Problem Length of room Width of room Start.

Slides:



Advertisements
Similar presentations
Modular Programming With Functions
Advertisements

Probability Three basic types of probability: Probability as counting
Lesson  In this investigation you will explore the predictability of random outcomes. You will use a familiar random process, the flip of a coin.
22C:19 Discrete Structures Discrete Probability Fall 2014 Sukumar Ghosh.
PROBABILITY  A fair six-sided die is rolled. What is the probability that the result is even?
1 Discrete Structures & Algorithms Discrete Probability.
Chapter 5 C Functions The best way to develop and maintain a large program is to divide it into several smaller program modules, each of which is more.
Lecture 25 Selection sort, reviewed Insertion sort, reviewed Merge sort Running time of merge sort, 2 ways to look at it Quicksort Course evaluations.
D. Roberts PHYS 121 University of Maryland Physics 121: Fundamentals of Physics I September 11, 2006.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Chapter 5 - Functions Outline 5.1Introduction 5.2Program.
 2007 Pearson Education, Inc. All rights reserved C Functions.
 2007 Pearson Education, Inc. All rights reserved C Functions.
1 Random numbers Random  completely unpredictable. No way to determine in advance what value will be chosen from a set of equally probable elements. Impossible.
C Lecture Notes Functions (Cont...). C Lecture Notes 5.8Calling Functions: Call by Value and Call by Reference Used when invoking functions Call by value.
Chapter 11: understanding randomness (Simulations)
An outcome is a possible result An event is a specific outcome Random means all outcomes are equally likely to occur or happen. random = fair A favorable.
Copyright © 2010 Pearson Education, Inc. Unit 3: Gathering Data Chapter 11 Understanding Randomness.
Standardized Score, probability & Normal Distribution
Copyright © 2000, Department of Systems and Computer Engineering, Carleton University 1 Introduction Structures are somewhat like an array in that.
Chapter 11 – Understanding Randomness 1. What is a random event? Nobody can guess the outcome before it happens. Let’s try an experiment. On the next page.
Copyright © Cengage Learning. All rights reserved.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. C How To Program - 4th edition Deitels Class 05 University.
Understanding Randomness Chapter 11. Why Be Random? What is it about chance outcomes being random that makes random selection seem fair? Two things: –
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 11 Understanding Randomness.
1.3 Simulations and Experimental Probability (Textbook Section 4.1)
Copyright © 2010, 2007, 2004 Pearson Education, Inc. Chapter 11 Understanding Randomness.
 2008 Pearson Education, Inc. All rights reserved Case Study: Random Number Generation C++ Standard Library function rand – Introduces the element.
AP STATISTICS Objective: Understanding Randomness Do Now: Take out any completed contracts, personal profiles, as well as your written design study. HW:
Chapter 11 Understanding Randomness. What is Randomness? Some things that are random: Rolling dice Shuffling cards Lotteries Bingo Flipping a coin.
C++ Programming Lecture 10 Functions – Part II
Loops Wrap Up 10/21/13. Topics *Sentinel Loops *Nested Loops *Random Numbers.
Statistics : Statistical Inference Krishna.V.Palem Kenneth and Audrey Kennedy Professor of Computing Department of Computer Science, Rice University 1.
Multiple Choice A carpenter wants to drill a hole that is just slightly larger than ¼ inch in diameter. Which of these is the smallest, but still greater.
CS221 Random Numbers. Random numbers are often very important in programming Suppose you are writing a program to play the game of roulette The numbers.
Copyright © 2002, Department of Systems and Computer Engineering, Carleton University CONTROL STRUCTURES Simple If: if (boolean exp) { statements.
1 A Balanced Introduction to Computer Science, 2/E David Reed, Creighton University ©2008 Pearson Prentice Hall ISBN Chapter 13 Conditional.
Copyright © 2002, Department of Systems and Computer Engineering, Carleton University 1 INPUT STREAMS ifstream xin; // declares an input stream.
Understanding Randomness.  Many phenomena in the world are random: ◦ Nobody can guess the outcome before it happens. ◦ When we want things to be fair,
Introduction Lecture 25 Section 6.1 Wed, Mar 22, 2006.
Copyright © 2000, Department of Systems and Computer Engineering, Carleton University 1 Introduction An array is a collection of identical boxes.
Random numbers in C++ Nobody knows what’s next....
CSci 162 Lecture 7 Martin van Bommel. Random Numbers Until now, all programs have behaved deterministically - completely predictable and repeatable based.
Chapter 10 Understanding Randomness. Why Be Random? What is it about chance outcomes being random that makes random selection seem fair? Two things: –
More on Logic Today we look at the for loop and then put all of this together to look at some more complex forms of logic that a program will need The.
6.3 Binomial and Geometric Random Variables
Chapter 3 Accelerated Motion. Introduction In this chapter we will examine acceleration and define it in terms of velocity. We will also solve problems.
Hot Dice Labettini PURPOSES: To illustrate the random nature of radioactive decay To define radioactive “half-life” To demonstrate that less stable elements.
Introduction Imagine the process for testing a new design for a propulsion system on the International Space Station. The project engineers wouldn’t perform.
Copyright © 2009 Pearson Education, Inc. Chapter 11 Understanding Randomness.
Statistics 11 Understanding Randomness. Example If you had a coin from someone, that they said ended up heads more often than tails, how would you test.
Review: Data and Probability _______ vs. _______.
1 Copyright © 2014, 2012, 2009 Pearson Education, Inc. Chapter 9 Understanding Randomness.
Intro CS – Probability and Random Numbers Lesson Plan 6a.
x y.
Intro CS – Probability and Random Numbers
5.3 The Central Limit Theorem
Random Numbers Until now, all programs have behaved deterministically - completely predictable and repeatable based on input values Some applications.
Four Points in a Row.
5.2 Probability
Hot Dice Labettini PURPOSES:
Chapter 5 - Functions Outline 5.1 Introduction
Chapter 5 - Functions Outline 5.1 Introduction
Which of the following numbers is between A.) 0.14 B.) 0.15 C.) 0.19 D.) 0.21 D.) 0.21 L F.
5.3 The Central Limit Theorem
COUNTING AND PROBABILITY
Sampling Distributions
5.3 The Central Limit Theorem
6.4b The Piano Method (aka: Sticks and Stones Method)
Hot Dice Labettini PURPOSES:
Chapter 11 Probability.
Presentation transcript:

Copyright © 2000, Department of Systems and Computer Engineering, Carleton University 1 A Simulation Problem Length of room Width of room Start position Black hole of death Door width The door is centrally located on its wall, and the black hole is located in the very middle of the room. All dimensions, including the diameter of the hole, are known. A drunken student starts at the position shown (mid- way along the wall). Assuming that he (or she) moves completely randomly, what the chances that he (or she) will plunge to his (or her) death before they get out of the door?

Copyright © 2000, Department of Systems and Computer Engineering, Carleton University 2 First Approach Take 100 drunken students. Place each in turn at the start position and let them stagger about until they either get out the door of fall down the hole. Have a clipboard handy and keep track of how many students fall down the hole. If “X” students fall down the hole, conclude that the probability of falling down the hole is X/100. Example: If 67 students fall down the hole, we can conclude that the probability of falling down the hole is 0.67 (67 %). Problems: - Students liable to sober up while awaiting their turn (so distorting the results). - Time consuming. - May be difficult to find enough students to achieve a reliable result (our results would be more convincing, for example, if we had 1000 instead of 100 students). - Others?

Copyright © 2000, Department of Systems and Computer Engineering, Carleton University 3 Second Approach Build a scale model of the room, scaled that one inch equals one step. Place a marker at the start position. Repeatedly roll two die (one red and one green) and move the marker in the direction selected (see table below) until the marker is moved either out the door or into the hole. Note the result on a clipboard, put the marker back at the start position, and repeat the entire process 99 more times. If “X” of the trials result in the marker getting moved into the hole, conclude that the probability of falling down the hole is X/100. Red DieGreen DieDirection Chosen 1, 2, 3 Towards door 1, 2, 34, 5, 6Away from door 4, 5, 61, 2, 3To the left (looking towards the door) 4, 5, 6 To the right (looking towards the door) Problems: - Still time consuming, though it may be possible to get some help from some of the students who survived the first approach.

Copyright © 2000, Department of Systems and Computer Engineering, Carleton University 4 Third Approach Convert the algorithm behind the second approach into a computer program and have a computer do all the work. Computers can’t roll dice, but they can generate “random” numbers. The quotation marks are because the numbers generated aren’t really truly random, though for our purposes the difference doesn’t matter much. The basic C++ random number function is “rand”. It returns a “random” integer value between 0 and RAND_MAX. int rand (void); // function prototype To use “rand”, “stdlib.h” must be included. Dividing values generated by “rand” by “RAND_MAX + 1.0” (note 1.0 and not 1) produces a “random” double value such that 0 <= value < 1. This is often a convenient first step, and many random number generators produce this kind of number to start with.

Copyright © 2000, Department of Systems and Computer Engineering, Carleton University 5 Picking a Direction In our case we want to randomly choose one of four possible directions. This can be done as follows: // returns an integer between 0 and 3 (inclusive) int pick_random_direction (void) { return rand() % 4; } This is clever, but in practice doesn’t work well because of deficiencies within “rand”. The directions chosen will not look very random but will instead cycle with a fairly short period. The following approach is better: // returns an integer between 0 and 3 (inclusive) int pick_random_direction () { double x; x = rand() / (RAND_MAX + 1.0); // 0 <= x < 1 x = x * 4; // now 0 <= x < 4 return (int) x; // 0 <= result <= 3 } This function can be reduced to a single line of code.

Copyright © 2000, Department of Systems and Computer Engineering, Carleton University 6 Room Co-Ordinates Length of room Width of room Start position = [ width / 2, 0 ] Black hole of death Door width x-axis y-axis origin = [ 0, 0 ] If we (conceptually) place a piece of graph paper over our room (as shown above), all positions can be expressed in terms of [ x, y ] co-ordinates. Initially the student is at [ width / 2, 0 ]. The centre of the hole is at [ width / 2, length / 2 ].

Copyright © 2000, Department of Systems and Computer Engineering, Carleton University 7 One Simulation The following pseudo-code defines a function that performs one simulation and returns its outcome: // set x, y to initial position x = width / 2; y = 0; // loop until the student has either gone down // the hole or out of the door (when we return) for (;;) { // pick a direction and work out new position. // place new position in new_x and new_y // see if the new position if within the hole. // if it is, return “down the hole” // see if the new position if out the door // if it is, return “out the door” // use new_x and new_y to update x and y. // in doing so, we must make sure that we don’t // move through a wall }

Copyright © 2000, Department of Systems and Computer Engineering, Carleton University 8 Function Details In the sample program (see hole.cpp), the function is defined as follows: int run_one_simulation (double width, double length, double door_width, double hole_diameter, long &seconds) ; The integer values that can be returned are given symbolic names by defining globally accessible constants: // values returned by "run_one_simulation" const int down_the_hole = 1, out_the_door = 2; These names are used (in place of the values 1 and 2) in both the function itself and the function which calls it, so making for a much more readable program. The call-by-reference parameter “seconds” is used to “return” the number of seconds taken for the student to either fall down the hole or get out the door.