Skip to content

Instantly share code, notes, and snippets.

@Tevinthuku
Created August 11, 2018 17:25
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Tevinthuku/96a9fd8cfa78d204e899bb4c2e3108f7 to your computer and use it in GitHub Desktop.
Save Tevinthuku/96a9fd8cfa78d204e899bb4c2e3108f7 to your computer and use it in GitHub Desktop.
This works just fine, though the values might not be similar to the ones given by the lec, thats because Im relying on random numbers of the specified range instead of hard coding the values
//Single Queue simulation
#include <iostream>
#include <fstream>
#include <stdlib.h> //contains the rand() function
#include <math.h>
#include <iomanip>
#include <ctime>
#include <cstdlib>
using namespace std;
// this function generates a random
//number of a certain range of values
// generateRandomInt :: Int -> Int -> Int
int generateRandomInt(int min, int max)
{
return min + (std::rand() % (max - min + 1));
}
//generateRandomIntervalTime :: Int ->Int
int generateRandomIntervalTime(int a)
{
switch (a)
{
case 1:
return generateRandomInt(0, 19);
case 2:
return generateRandomInt(20, 44);
case 3:
return generateRandomInt(45, 74);
case 4:
return generateRandomInt(75, 89);
case 5:
return generateRandomInt(90, 99);
default:
return generateRandomInt(0, 19);
}
}
//generateRandomServiceTime :: Int -> Int
int generateRandomServiceTime(int a)
{
switch (a)
{
case 1:
return generateRandomInt(0, 9);
case 2:
return generateRandomInt(10, 24);
case 3:
return generateRandomInt(25, 59);
case 4:
return generateRandomInt(60, 74);
case 5:
return generateRandomInt(75, 89);
case 6:
return generateRandomInt(90, 99);
default:
return generateRandomInt(0, 9);
}
}
//recursion is divine
//numberinQue :: Int -> Int
int numberinQue(int index, int endtimes[], int arrival)
{
if (index == 0)
return 0;
// if arrivaltime is less than endtime[i]
//then it means there are other pple in the queue so add 1
// otherwise add zero..
return arrival < endtimes[index]
? 1 + numberinQue(index - 1, endtimes, arrival)
: 0 + numberinQue(index - 1, endtimes, arrival);
}
// main fn
int main()
{
ofstream outfile("output.txt", ios::out);
outfile << "\n Customer" << '\t' << "RN(Arr)" << '\t' << "TimeBtwnArr" << '\t' << "Arrivaltimeonclock"
<< "servicetime" << '\t' << "rnservice" << '\t' << "starttime" << '\t' << "Endtime" << '\t' << "NumberInQueue" << '\t' << "ServerIdletime"
<< "Delays \n";
// default arrivaltime
int arrivalonclock = 100;
int endtime = 100;
int endtimesarr[15] = {0};
int serverIdleTime;
int numberinqueval, delays;
// summations
int sumservicetime = 0, sumnumberinqueue = 0, serveridlesum = 0, sumdelays = 0;
for (int i = 0; i < 15; i++)
{
// values to be computed
// timebtwnarrival is generated from 1-5
int timebtwnarrival = generateRandomInt(1, 5);
int rnainterarrival = generateRandomIntervalTime(timebtwnarrival);
// the new arrivalonclock is the previous arrival time plus timebtwnarrival
arrivalonclock = arrivalonclock + timebtwnarrival;
// the servicetime is generated randomly from 1-6
int servicetime = generateRandomInt(1, 6);
int rnservice = generateRandomServiceTime(servicetime);
// add to the servicetime summation value
sumservicetime = sumservicetime + servicetime;
// deal with starttime and end time
// this if condition is for the first client
// we compute the starttime from endtime
if (i == 1)
{
// only this condition holds for the first item
endtime = endtime + servicetime + timebtwnarrival;
}
else
{
endtime = endtime + servicetime;
}
// starttime == endtime - servicetime
//... we will subtract directly in the outfile
// push val to endtimesarr
endtimesarr[i] = endtime;
// compute number of guys in que
numberinqueval = numberinQue(i, endtimesarr, arrivalonclock);
//sumnumberinqueue
sumnumberinqueue = sumnumberinqueue + numberinqueval;
// server idle time
//all except the first client default to zero
// for first client it is arrivaltime - () // ()-> unit or identity which is 100
if (i == 0)
{
serverIdleTime = arrivalonclock - 100;
}
else
{
serverIdleTime = 0;
}
// add all the idle times up
serveridlesum = serveridlesum + serverIdleTime;
// finally on delays
// delays == starttime - arrivaltimeonclock
if (i == 0)
{
delays = 0;
}
else
{
delays = (endtime - servicetime) - arrivalonclock;
}
sumdelays = sumdelays + delays;
outfile
<< i + 1 << '\t' << rnainterarrival << '\t' << timebtwnarrival << '\t' << (float)(arrivalonclock) / 100.0 << '\t' << servicetime << '\t' << rnservice << '\t' << (float)(endtime - servicetime) / 100.0 << '\t' << (float)(endtime) / 100.0 << '\t' << numberinqueval << '\t' << serverIdleTime << '\t' << delays << "\n";
}
outfile << "Server Utilization " << (((float)(sumservicetime) - (float)(serveridlesum)) / (float)(sumservicetime)) << setprecision(4) << "\n";
outfile << "Average Delays " << (float)(sumdelays) / (float)(15) << "\n";
outfile << "Average number of customers in queue " << (float)(sumnumberinqueue) / (float)(sumservicetime) << "\n";
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment