Queue Simulation

Here is a version of the queue simulation from the video. At each time step, x people join the queue and y people leave the queue, where x and y are both Poisson(5) random variables.

(* initialize variables *)
queueSize=1;
time=0;

(* main loop *)
While[queueSize>0,
time+=1;
x=RandomVariate[PoissonDistribution[5]]  (* x people join the queue *);
queueSize=queueSize+x;
y =RandomVariate[PoissonDistribution[5]](* y people leave the queue *);
If[y>queueSize,queueSize=0,queueSize = queueSize-y];
Print["at time ",time," the queue contains: ",queueSize]
]
Print["time until the queue is empty: ",time]

Placing the simulation inside of a “module” allows us to easily run it lots of times and collect the results in a list.

queueSim[]:=Module[{},
(* initialize variables *)
queueSize=1;
time=0;

(* main loop *)
While[queueSize>0,
time+=1;
x=RandomVariate[PoissonDistribution[5]]  (* x people join the queue *);
queueSize=queueSize+x;
If[queueSize > 100,queueSize=100];
y =RandomVariate[PoissonDistribution[5]](* y people leave the queue *);
If[y>queueSize,queueSize=0,queueSize = queueSize-y];
(*Print["at time ",time," the queue contains: ",queueSize]*)
];
(*Print["time until the queue is empty: ",time];*)
time
]

Try it out:

queueSim[]

results = Table[queueSim[],100000];

Mean[results]//N