Presentation is loading. Please wait.

Presentation is loading. Please wait.

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

Similar presentations


Presentation on theme: "91.166 Copyright © 2000, Department of Systems and Computer Engineering, Carleton University 1 A Simulation Problem Length of room Width of room Start."— Presentation transcript:

1 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?

2 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?

3 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.

4 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.

5 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.

6 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 ].

7 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 }

8 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.


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

Similar presentations


Ads by Google