The idea: jump from one event to the next
In a bank, the only moments that matter are when a customer walks in and when one leaves. Between them nothing changes: the line stays the same length and the teller stays busy or idle. A discrete-event simulation uses that. It keeps a list of the things that will happen, each with a time, and runs a loop of three lines: take the earliest event, move the clock to its time, and run the code for that kind of event. That code may add new events to the list.
The page simulates the classic example from Law and Kelton's textbook: a bank with one teller. Customers arrive on average every 1.0 minute and need on average 0.8 minutes of service. The run stops when 20 customers have finished waiting. Demo: start the bank runs the start-up and the first two events, one step at a time. The same queue, run for 100 000 customers and compared with the formulas, is on the M/M/1 queue page.
The parts of the engine
| Part | In the bank |
|---|---|
| Clock | the simulated time in minutes; it only moves when an event is taken |
| Future event list | pending events (time, ARRIVAL or DEPARTURE, customer) |
| State | teller busy or idle, B(t); customers waiting, Q(t); their arrival times |
| Event handlers | one routine per event type: what changes, what gets scheduled |
| Statistical counters | customers delayed, total delay, area under Q(t), area under B(t) |
| Random-number generator | gives the gaps between arrivals and the service times |
The future event list is a min-heap
The loop always needs the earliest event, so the list is kept as a min-heap on time: the earliest event sits at the root, slot [0], and the parent of slot [i] is slot [(i − 1)/2]. A new event goes into the first free slot and sifts up while it is earlier than its parent. Taking the root moves the last entry to the top and sifts it down. Both cost O(log n) for n pending events. Ties are broken by the order of scheduling, so a run is always the same. The heap itself is on the priority queue page.
In the first event, C1 arrives at 8.020. Its handler schedules the next arrival, C2 at 8.665, and C1's departure at 8.266. The departure is earlier, so it sifts up and becomes the root. With one teller the list never holds more than two events: the next arrival and the departure of the customer in service. A bank with c tellers holds up to c + 1, and a network simulator holds thousands, which is where the heap pays off.
The two event handlers
| Event | Change the state | Schedule |
|---|---|---|
| ARRIVAL of Ck | teller idle: Ck is served at once (delay 0) and the teller becomes busy; teller busy: Ck joins the tail of the queue with its arrival time | the next arrival, C(k+1); Ck's departure if it was served |
| DEPARTURE of Ck | queue empty: the teller becomes idle; otherwise the head Cj goes to the teller, and its delay = now − its arrival time | Cj's departure |
An arrival always schedules the next arrival. That one line keeps customers coming for the whole run; the list starts with a single event, the first arrival, scheduled by the initialisation at clock 0. Demo: a queue builds shows C6, C7 and C8 arriving while C5 is served, and then C6 reaching the teller after waiting 0.429 minutes.
Statistical counters and the area under Q(t)
A delay is easy to count: when a customer reaches the teller, add the wait to the total. The average queue length needs more care, because it is an average over time. Q(t) is a step function: it is constant between events. So just before each event, the engine adds Q × (time since the last event) to area Q, and B × (time since the last event) to area B. At the end, average Q = area Q ÷ T and utilisation = area B ÷ T.
One long service shows it well: C14 is served from 22.892 to 25.909, 3.017 minutes, while 4 people wait. That single gap adds 4 × 3.017 = 12.068 to area Q (Demo: one long service). After 20 customers, the run has handled 39 events and the clock reads 27.004.
| Result after 20 customers | How it is computed | Value |
|---|---|---|
| mean delay in the queue | total delay ÷ 20 = 50.620 ÷ 20 | 2.531 min |
| average number waiting | area Q ÷ T = 50.620 ÷ 27.004 | 1.875 |
| teller utilisation | area B ÷ T = 16.262 ÷ 27.004 | 0.602 |
| longest queue | largest Q seen | 7 |
Area Q equals the total delay here (both 50.620). That is no accident: each waiting customer adds one to Q for as long as they wait, so the area is the sum of the waits. It is Little's law in small form.
Where the random times come from
The numbers come from a Lehmer generator, a linear congruential generator with x(k+1) = 16807 · x(k) mod (2³¹ − 1). Starting from the seed x(0) = 42, it gives 705 894, then 1 126 542 223, then 1 579 310 009. Dividing by 2³¹ − 1 gives U between 0 and 1: 0.000329, 0.524587, 0.735424. The same seed always gives the same stream, so a run can be repeated exactly.
The inverse transform turns U into an exponential time: X = −ln(U) × mean. A U near 1 gives a short time and a U near 0 a long one, with the right probabilities (the exponential distribution page shows why). The first U is tiny because 42 × 16807 is far smaller than 2³¹ − 1, so the first gap is −ln(0.000329) × 1.0 = 8.020 minutes: nobody comes in for eight minutes. The event-driven clock jumps over them in one step.
Event-driven vs fixed time step
The other way to run a model is a fixed time step: move the clock by Δt every pass and check whether anything is due. It is simple, and it fits models that change all the time, like physics. For a bank it wastes passes, and it moves each event to the next tick, so the whole history shifts.
| Loop (same bank, same random numbers) | Passes | Idle passes | Mean delay | Average Q |
|---|---|---|---|---|
| event-driven | 39 | 0 | 2.531 min | 1.875 |
| fixed step Δt = 0.1 min | 283 | 247 | 1.660 min | 1.173 |
| fixed step Δt = 0.5 min | 66 | 36 | 0.600 min | 0.364 |
With Δt = 0.1 the first 80 passes find nothing to do (Demo: fixed time step Δt = 0.1). With Δt = 0.5 the loop is cheaper, but arrivals and departures that fall in the same half-minute are lumped together and the mean delay drops to less than a quarter of the true value. A smaller step comes closer but needs more passes, almost all of them empty. The event-driven loop gets the exact times with one pass per event. A browser's event loop works the same way: it sleeps until the next task or timer is due instead of polling.
Reading the result
For this bank in the long run the M/M/1 formula gives a mean delay of ρ / (μ − λ) = 0.8 / 0.25 = 3.2 minutes. The run gives 2.531. Twenty customers are too few, and the run starts with an empty bank, which pulls the early delays down. How long to run, what to throw away at the start, and how to put an error bar on the answer is the subject of simulation output analysis. For networks of queues, mean value analysis gives the averages without simulating at all.
See also CPU scheduling, where an operating system's scheduler is itself driven by events: arrivals, completions and timer interrupts.
See also Mean Value Analysis, which gives exact mean values for a closed queueing network without simulating it.
See also 2^k Factorial Design for choosing which simulation runs to make and splitting their variation among the factors.
See also Pooled Queues: M/M/c, which runs three queueing systems on the same seeded customers and checks the results against Erlang C.
What the page leaves out
The page keeps one teller, one stream of random numbers and a stop after 20 customers. Real simulators use several independent streams (one per source of randomness) so that changing one part of the model does not shift the others, and better generators than this 1988 "minimal standard" one, such as MRG32k3a or the Mersenne Twister. It does not show other event-list structures (calendar queues, ladder queues), process-oriented simulation where each customer is a coroutine (as in SimPy), events that cancel other events, a fixed end time with an END event, or parallel simulation across machines.
On the mind map
References
Discrete-event simulation: clock, event list, state, statistics (Wikipedia)
Park and Miller: Random number generators: good ones are hard to find (CACM, 1988)
Lehmer random number generator (MINSTD, a = 16807, m = 2³¹ − 1)
SimPy: a process-based discrete-event simulation library for Python