"""One room, random arrivals: the average wait in line, in visit-lengths. Each patient's wait follows from the one before (Lindley's recursion): wait[n+1] = max(0, wait[n] + visit[n] - gap[n+1]) No queueing formula is used. Save as queue_sim.py and run: python3 queue_sim.py It takes a few minutes; the page reports the five runs at 90% busy.""" import random def mean_wait(busy, fixed, patients, seed): rng = random.Random(seed) wait = total = 0.0 for _ in range(patients): total += wait visit = 1.0 if fixed else rng.expovariate(1.0) # mean visit length = 1 gap = rng.expovariate(busy) # mean gap between arrivals = 1/busy wait = max(0.0, wait + visit - gap) return total / patients if __name__ == '__main__': N = 2_000_000 for busy in (0.8, 0.9, 0.95): for fixed in (False, True): runs = [mean_wait(busy, fixed, N, seed) for seed in range(1, 6)] print(f'busy {busy:.2f} {"fixed " if fixed else "random"} visits seeds 1-5: ' + ' '.join(f'{r:.2f}' for r in runs) + f' mean {sum(runs)/5:.2f}')