We are going to code a very simple queuing model. For this simulation we are going to assume a single server (where the people are processed), that people arrive following a Poisson distribution and the service time is a random number with an exponential distribution.
See the code running here: example simple queuing model.
Code was written by Ted Harris, Dec 2007 for Analyticalway.com
Questions and comments to: email@example.com
First, we need to decide which distribution to use fro the arrival and service rates. Assuming a Poisson distribution for arrival times leads to an arrival rate generated from an exponential function. For service time we can also assume an exponential function.
Generating random number from an exponential functions is easy. We just need to solve: f(x) = -exp^alpha*x for x giving us: x = (-1/alpha) *log(f(x) ). Generate f(x) from a uniform distribution and you are done.
var intR = Math.random();
var intX = (-1/intAlpha) * Math.log(intR);
/* Here is the core to the code that runs the simuation.*/
function runsim ()
/*First we declare dynamic arrays to hold our cases and results. */
var aryCases = new Array();
var aryResults = new Array();
var intTotCases= 100; /* The total number of cases you want to simulate.. */
var fltArrival = .5; /* This variables sets the average arrival rate. */
var fltWait = .5; /* This sets the average wait time, */
/* Next we populate the arrival and wait times for each case. */
for (i = 0; i < intTotCases; i++)
aryCases[aryCases.length] = new Array(i, exponential(fltArrival) ,exponential(fltWait) );
Since we are assuming one server and one queue this is a relatively simple set up. That is dependant on the first case.
Now we can set the arrival and departure time for the first case. This on is simple:
Arrival = Exponential random number
Departure = Arrival + Exponential random number.
aryResults[aryResults.length] = new Array(1, aryCases , aryCases + aryCases ,aryCases ,aryCases );
Now we set the arrival and departure times for the rest of the cases.
Arrival = Arrival time of previous queue member + Exponential random number
Departure = maxiumn value between Arrival + Exponential random number and exit time of previous queue memeber + Exponential random number .
for (i = 1; i < aryCases.length; i++)
aryResults[aryResults.length] = new Array(i, aryResults[i-1]+ aryCases[i] , Math.max( (aryResults[i-1]+ aryCases[i]+ aryCases[i]) , (aryResults[i-1]+ aryCases[i]) ) );
/* Now write the results to the webpage.
/* This function write the results. */
function writeresults( aryResults )
/*First, find the target object in the document (webpage) to over-write. */
var objParent= document.getElementById( results );
/* Create a new version of that object to be replaced.*/
var objNewParent = document.createElement( span );
/* Set the object ID to match that of the object we are replacing.*/
objNewParent.id = objParent.id;
/*Replace the object with our new object which has our new results writen to it. */
/*Now, clear out any child objects (content) associated with the old parent . */
/* Write the header to the output.*/
objNewParent.innerHTML += <p> Arrives , Departs </p> ;
/*Now, loop through the results table and write the contents to the new span we ahev created, */
for (i = 0; i < aryResults.length; i++)
objNewParent.innerHTML += <p> + aryResults[i] + , + aryResults[i] + </p> ;
<!– Create a button with an onclick event to run the code.–>
<button onclick = runsim(); > Run simulation </button>
<!– Add a break to make the code look a little better–>
<span id = results >
Now you can play around with parameter and copy paste the output to Excel to do further research. In some version of Excel the columns will not be properly placed. To correct for this copy paste the output to a text document then change the file extension to csv. Excel should now open the file correctly. One fun thing to do is slowly increase the time to be served while keeping the arrival rate constant and watch how quickly the average wait time increases.