LESSON 15.03 · 6 STAGES · 3 CONTROLS · 3 CHECKS
Stochastic simulation and rare events
Watch random arrivals fill a queue, then count the rare runs that overflow it.
EXPLAINER · 15.03 · QUEUE SIM
01
Stage 1 of 6 · Random arrivals
Requests arrive at random times. This run gets 139 requests in 60 s.
Explain it simply
Each tick is one request. The ticks come at random times, so some seconds get many and some get none. Over the minute, this run averages 2.32 requests per second. The long-run average, called lambda, is 2.2.
A time strip from 0 to 60 s with 139 ticks, one per request.
arrivals and rateserver and queue limitqueue and estimatesoverload and dropsexact chance (plain dashed line)
Toy: one queue for 60 s, rate 2.2 per s, server 3 per s, 16 places
HOVER OR TAP A SHAPE TO SEE ITS NUMBERS
0.6
The chance the rate keeps its level each second.
1,000
How many 60 s runs the batch holds.
1
The seed that fixes every random draw.
MATH · STAGE 1HOVER A SYMBOL OR A SHAPE
Each second's count of requests is a Poisson draw at the current rate.
Show the code ▾Hide the code ▴ · Try it yourself · Python 3
import math
RHO, RUNS, SEED = 0.6, 1000, 1
LAM, SERVE, LIMIT, SECONDS = 2.2, 3, 16, 60
LEVELS = [0.4, 0.7, 1, 1.3, 1.6] # noise levels, mean 1
WEIGHTS = [1/16, 4/16, 6/16, 4/16, 1/16]
def mulberry32(seed): # the lesson's seeded generator
a, M = seed, 0xFFFFFFFF
def rand():
nonlocal a
a = (a + 0x6D2B79F5) & M
t = ((a ^ (a >> 15)) * (1 | a)) & M
t = ((t + (((t ^ (t >> 7)) * (61 | t)) & M)) & M) ^ t
return ((t ^ (t >> 14)) & M) / 2**32
return rand
def level_of(u):
acc = 0.0
for j, w in enumerate(WEIGHTS):
acc += w
if u < acc:
return j
return len(LEVELS) - 1
def poisson_of(mean, u): # inversion of one uniform
k, p = 0, math.exp(-mean)
F = p
while u >= F and k < 200:
k += 1
p *= mean / k
F += p
return k
def simulate(r): # one 60 s run; returns (arrivals, peak, drops)
rand = mulberry32(1503 * 1000 + SEED * 100000 + r)
q = j = peak = drops = total = 0
for t in range(SECONDS):
u_stay, u_level = rand(), rand()
j = j if (t > 0 and u_stay < RHO) else level_of(u_level)
a = poisson_of(LAM * LEVELS[j], rand())
q = max(0, q + a - SERVE)
if q > LIMIT:
drops += q - LIMIT
q = LIMIT
peak, total = max(peak, q), total + a
return total, peak, drops
total, peak, drops = simulate(0)
print(total, peak, drops) # 139 12 0
hits = sum(1 for r in range(RUNS) if simulate(r)[2] > 0)
print(hits, hits / RUNS) # 12 0.012
z = 1.96 # 95% Wilson range
p, n = hits / RUNS, RUNS
mid = (p + z*z / (2*n)) / (1 + z*z / n)
half = z * math.sqrt(p*(1 - p) / n + z*z / (4*n*n)) / (1 + z*z / n)
print(round(max(0, mid - half), 4), round(mid + half, 4)) # 0.0069 0.0209
# the exact chance: carry P(queue, level) forward, drops absorbed
P = [[WEIGHTS[j] if q == 0 else 0.0 for j in range(5)] for q in range(LIMIT + 1)]
dropped = 0.0
for t in range(SECONDS):
N = [[0.0] * 5 for _ in range(LIMIT + 1)]
for q in range(LIMIT + 1):
for j in range(5):
mean = LAM * LEVELS[j]
for a in range(LIMIT + SERVE - q + 1):
pa = P[q][j] * math.exp(-mean) * mean**a / math.factorial(a)
q2 = max(0, q + a - SERVE)
for j2 in range(5):
N[q2][j2] += pa * ((RHO if j2 == j else 0) + (1 - RHO) * WEIGHTS[j2])
dropped -= pa
dropped += P[q][j]
P = N
print(round(dropped, 4)) # 0.0148
Copy it into a file and run it. The batch of 10,000 takes a few seconds.
Common mix-ups
MYTHNo drops in 100 runs means drops never happen.
FIXAt ρ = 0 the exact chance is 0.21%. With seed 1, 10,000 runs find 13 drops.
MYTHNoise with the same average rate gives the same risk.
FIXBoth average 2.2 requests per second. At ρ = 0.9 the top rate holds 10.7 s, long enough to fill the queue: this run drops 5.
Predict, then reveal
0 / 3 REVEALEDQ01If the noise keeps its level longer, does the drop chance rise or fall?
1 · GUESS2 · TRY IT IN THE DIAGRAM
Q02At ρ = 0.6 with 100 runs, does seed 3 match seed 1?
1 · GUESS2 · TRY IT IN THE DIAGRAM
Q03At ρ = 0.6, seed 1, how much shorter is the 95% range with 10 times the runs?
1 · GUESS2 · TRY IT IN THE DIAGRAM
Takeaways
- A stochastic simulation repeats random runs and reports the spread.
- Correlated noise makes long bursts, so overflows get more common.
- A rare event of chance p needs about 100/p runs for a 10% error.