Simulation Example

This month we are going to take a stab at programming a simple queuing model in JavaScript.  First, why JavaScript and not R or SAS?  JavaScript is a good language to use for programming examples because nearly anyone with a computer and browser can play around with it. Another good reason is that while JavaScript is not JAVA it is very similar to JAVA and to other languages such as C++ and C#.  Learning JavaScript will help you in coding in those languages as well.

The model:

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
Questions and comments to:
<script type= text/javascript >

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.

function exponential(intAlpha)
var intR = Math.random();
var intX = (-1/intAlpha) * Math.log(intR);
return Math.round(intX);

/* 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[0][1] , aryCases[0][1] + aryCases[0][2] ,aryCases[0][1] ,aryCases[0][2] );

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][1]+ aryCases[i][1] , Math.max( (aryResults[i-1][1]+ aryCases[i][1]+ aryCases[i][2]) , (aryResults[i-1][2]+ aryCases[i][2]) ) );

/* 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.*/ =;
/*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 . */
while (objParent.firstChild)

/* 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][1] + , + aryResults[i][2] + </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–>

<!– Create a span to write the output to. It is important to set the id for the span so JAVAScript can find it latter. –>
<span id = results >
</span >

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.

Further Reading: