The Program Life Cycle.

Download Report

Transcript The Program Life Cycle.

Day 12.
0. Intro.
1. Circular queues.
2. Concepts of simulation.
3. “Random” numbers.
0. Intro.
Today we will look at circular queues in
detail, and the goals of simulation
programs.
Thursday, we will explore program 4 in depth
(this is the first complex program, which
requires a lot of thinking through).
1. Circular queues.






A. Concept.
Recall the 3 basic array-based queues:
1) fixed front.
2) Variable front.
3) Circular queue.
See diagrams.
Circular queue implementation.
 B. In-depth circular queue
implementation.
 See diagrams and:
 1) the template class Tempqueue.cs
 2) The driver Queue_Driver.cs.
 Note how the access methods and observers
reflect the circular queue design.
2. 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.
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.
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 ();
}
3. Random numbers.
But, how do I simulate something that may or
may not happen, such as the arrival of a
customer for a queue?
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.
 See Randsim.cs
 Random RandomObject = new Random(100);
 for (int i = 0; i < 30; i++)

if ((int)(RandomObject.NextDouble() * 100) < 40)

Console.Write("The event occurred\n");

else

Console.Write("The event did not occur\n");
See clock diagram.
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.