190 likes | 282 Views
Day 13. 0. Intro. 1. Review concepts of simulation. 2. “Random” numbers. 3. Dynamic queues. 4. Lab using RANDSIM.CPP. 0. Intro. Make sure you have kept a copy of 4A on a drive somewhere.
E N D
Day 13. 0. Intro. 1. Review concepts of simulation. 2. “Random” numbers. 3. Dynamic queues. 4. Lab using RANDSIM.CPP.
0. Intro. Make sure you have kept a copy of 4A on a drive somewhere. See Assignment 4B—requires additional concepts presented today, and will work through the algorithm carefully on Tuesday after Spring Break. CSC Get Together is Today!!!
1. Review Concepts of Simulation. 3 definitions. Def 1. Simulation in general. A simulation is a model of (A) a real world object AND (B) the processes affecting it. Changes in the model represent changes in the object.
Review Concepts of Simulation. Def 2. Computer Simulation. A computer simulation is a simulation (as defined by Def. 1) using: (A) 1 or more data structures to represent real world objects e.g. queues, servers; (B) operations on data structures to represent changes in the objects e.g. enq, deq, updatequeue, EngageServer.
Review Concepts of Simulation. Def 3. Time-driven simulation. A time-driven simulation is a computer simulation (as defined by Def. 2) driven by a counter that represents a clock: Clock = 0; while (Clock < SimulationTimeLimit){ Clock++; // clock ticks RunSimulation (); }
2. Random numbers. But, you say, how, oh, how, do I simulate something that may or may not happen, such as the arrival of a customer for a queue. You may well ask. And well you may. The problem is computer operations are deterministic—either they must happen or they must NOT happen.
Random numbers. How then, can we simulate something that may only probably happen? E.g., what if, on average, there is a 50% chance a new customer arrives each minute on Saturday afternoon, but only a 10% chance a new customer arrives on Monday morning?
Random numbers. In truth, the computer does not really allow any chance events (it is a deterministic device), but we can simulate probabilities using the “random” number generator. This is a paradox, because nothing the computer does is truly random, since everything it does is algorithmic. Truly random events are things like the decay of a radioactive nucleus, or other quantum events (if QM is true).
Random numbers. However, we can capture important characteristics of random events, even using an algorithm. We can then generate a “pseudo-random” sequence of numbers / events—meaning? To simulate truly random numbers we need to capture 2 key ideas.
Random numbers. (1) Internally, a sequence is random-looking if there is no simple predictable pattern e.g. 1 3 5 7 does not even look random, because it conforms to the rule n’ = n+2. (2) Externally, a sequence seems random if each number is equally likely to occur (there is no bias), e.g. 2 2 5 2 does not look random.
Random numbers. In fact, though, one cannot definitively say whether a finite sequence is intrinsically random or not—it depends on how it is generated: 2 2 5 2 might be generated by a fluke. Also 7 3 9 1 might seem random with 4 digits, but then repeat indefinitely according to an algorithm.
Random numbers. The only numbers which we are confident are random are transcendentals like Pi, which has been checked for billions of places and has no predictable pattern or significant repeats. However, for simulations, true randomness is not required, since any simulation will last a finite amount of time, and we cannot distinguish random/non-random absolutely in this case.
Random numbers. All we need for a finite simulation is number sequences that look random. Just as for a geometry program, a finite approximation for Pi suffices, likewise for “random numbers”. And for this, there are clever algorithms, that typically start from a seed that uses a finite approximation to Pi or something else believed to be truly random.
Random numbers. What does C++ have to offer? Within <stdlib.h> there is a pseudo-random number generator which can be accessed by rand(). The function rand() returns a pseudo-random number between 0 and RAND_MAX. For PCs, RAND_MAX is 32767.
Random numbers. See clock diagram. How then do we simulate a 20% chance? if (rand() < int (0.2 * RAND_MAX)) cout << “The event occurred”; else cout << “The event did not occur.”;
Random numbers. If repeatedly called, this will generate a pseudo-random sequence. But if we run the same program twice? Get the same sequence. Why? Same algorithm operating on the same seed = initial value.
Random numbers. If we want to change this, can re-set the seed, using srand (Num); However, for 4B you don’t need to do this. See RANDSIM.CPP
3. Dynamic queues (Batman). For assignment 4A and 4B, you are using a static circular queue. An alternative would be to use a dynamic queue. See diagrams.
4. Lab: using RANDSIM.CPP. See what happens with the following probabilities: .2 .5 .8