SlideShare a Scribd company logo
1 of 16
Download to read offline
Task 1.1
Generate 1000 independent uniformly distributed random numbers (in range 0 to 1). Calculate the
sample mean and sample standard deviation of these numbers.
#include<math.h>
#include <stdio.h>
#include <stdlib.h>
#include <limits.h>
#include<iostream>
#include <iostream>
#include <string>
#include <cstdlib>
#include <iomanip>
#include <time.h>
using namespace std;
int main()
{
float Rand_Value[1000];
srand( (unsigned)time( NULL ) );
for (int i = 0; i < 1000; i++)
{
Rand_Value[i] = (float) rand()/RAND_MAX ;
//cout << (float) rand()/RAND_MAX << endl;
}
for (int j = 0; j < 1000; j++)
{
cout<<Rand_Value[j]<<endl; //= (float) rand()/RAND_MAX ;
//cout << (float) rand()/RAND_MAX << endl;
}
cin.get();
return 0;
}
Task 1.2
Generate 1000 independent exponentially distributed random numbers with mean 35.0. Calculate the
sample mean and sample standard deviation of these numbers.
-Since I preferred compiling C++ in windows using Visual studio I was not able to use “drand48” to
produce exponentially distributed random numbers so I implemented another method using “rand”
#include<math.h>
#include <stdio.h>
#include <stdlib.h>
#include <limits.h>
#include<iostream>
#include<math.h>
#include <iostream>
#include <string>
#include <cstdlib>
#include <iomanip>
#include <time.h>
using namespace std;
int main()
{
const int N = 1000;
float Rand_Value_exp[N];
float Rand_Value[N];
float mean = 35.0;
float exp,var=0, dev = 0;
float sum = 0;
srand( (unsigned)time( NULL ) );
for (int i = 0; i < N; i++)
{
Rand_Value_exp[i] = ( (int)(- log((float) rand() / RAND_MAX) *
mean + 0.5) );
//Rand_Value[i] = (float) rand()/ RAND_MAX;
sum = sum+Rand_Value_exp[i];
}
for (int j = 0; j < N; j++)
{
cout<<Rand_Value_exp[j]<<endl; //= (float) rand()/RAND_MAX ;
}
cout<<"Sample mean is equal:n";
exp = sum/N;
cout<<sum/N;
for(int k=0; k < N;k++)
{
var = pow((Rand_Value_exp[k] - exp),2) + var;
}
dev = sqrt(var)/N;
cout<<"n";
cout<<"Standard deviation="<<dev;
cin.get();
return 0;
}
Task 2
What conclusions did you draw from Lab 1 concerning:
1. Accuracy vs number of events for a fixed occupancy (or utilisation)?
2. Accuracy vs occupancy for a fixed number of events?
In a Single server Queuing modeling in fixed utilization ( where the ratio of arrival rate and departure
rate is fixed ) it can be seen that the higher accurate number can be produced when there are less
events .it means for example in modeling 1 case where the exact solution is known the accuracy is at its
highest.
In case of modeling for fixed number of events I have tested the program with 100 events with different
arrival rate and holding time ratio and It seems the accuracy is higher for higher utilization.
Task 3
Task 1 in Lab 2 asks for a comparison of the mean queue lengths of M/M/1 and M/D/1 queues. What
do you conclude? Give some evidence.
The difference between M/M/1 and M/D/1 is Deterministic service time which is constant, rather than
being exponentially distributed. So we modified the code by eliminating the “negexp” function and
compared the results .we observed that the mean queue length for the system with more random case
(M/M/1, which has both random arrivals and random service) is greater than the less random case
(M/D/1) which can be explained by looking at the formula for:
Average length of Queue in M/M/1 system
Average length of Queue in M/M/D system
Task 3.2
Summarize the code changes required between the M/M/1 ssq.c simulation and the M/M/N/N
simulation of Task 2 in Lab 2.
In this task the user inputs the numbers of servers to programs ( “NumberOfServer” ) and programs
simply multiples the numbers of packets being serviced by this n server.it also decrements the
DEPARTURE packets based on n. for example in the case of 3 servers 3 packets will be serviced and once
they depart the system 3 packets will be decremented from the list .
/* program ssq.c */
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <limits.h>
#include <iostream>
( )ρ
ρ
−
=
1
2
Q
( )ρ
ρ
−
=
12
2
Q
using namespace std;
#define NEW(type) (type *) malloc(sizeof(type))
#define ARRIVAL 1
#define DEPARTURE 2
/* Event by event simulation of a single server queue with infinite
waiting
room */
int
gmt, /* absolute time */
q, /* number of customers in the system */
narr, /* number of arrivals */
q_sum, /* sum of queue lengths at arrival instants */
iat, /* mean interarrival time */
service_time, /* mean holding time */
total_events; /* number of events to be simulated */
int NumberOfServer; /* Number of Server */
unsigned int
seed; /* seed for the random number generator */
typedef struct schedule_info{
int time; /* Time that event occurs */
int event_type; /* Type of event */
struct schedule_info *next; /* Pointer to next item in linked
list */
} EVENTLIST;
struct schedule_info
*head,
*tail;
int act(void);
int negexp(int);
void arrival(void);
void departure(void);
void schedule(int, int);
void sim_init(void);
/*************************************************************************
*/
int main(){
sim_init();
while (narr < total_events){
switch (act()){
case ARRIVAL:
arrival();
break;
case DEPARTURE:
departure();
break;
default:
printf("error in act proceduren");
exit(1);
break;
} /* end switch */
} /* end while */
printf("The mean queue length seen by arriving customers is: %8.4fn",
((float) q_sum) / narr);
int n; cin>>n;
return(0);
} /* end main */
/*************************************************************************
*/
int negexp(int mean) /* returns a negexp rv with mean `mean' */
{
return ( (int)(- log((float) rand() / RAND_MAX) * mean + 0.5) );
}
/*************************************************************************
*/
void arrival() /* a customer arrives */
{
//defines number of servers
narr += NumberOfServer; /* keep tally of number of
arrivals */
q_sum += q;
schedule(negexp(iat), ARRIVAL); /* schedule the next arrival */
//accumulates numbers of customers in the system based on number of
servers
q += NumberOfServer;
//marks the last node in the server as DEPARTURE
if (q == NumberOfServer)
schedule(negexp(service_time), DEPARTURE);
}
/*************************************************************************
*/
void departure() /* a customer departs */
{
//Decrements number of customers in the system after customer departs
q -= NumberOfServer;
if (q > 0)
schedule(negexp(service_time), DEPARTURE);
}
/*************************************************************************
*/
/*************************************************************************
*/
void schedule(int time_interval, int event) /* Schedules an event of type
*/
/* 'event' at time
'time_interval'
in the future */
{
int
event_time;
struct schedule_info
*x,
*t;
event_time = gmt + time_interval;
t = NEW(EVENTLIST);
for(x=head ; x->next->time<event_time && x->next!=tail ; x=x->next);
t->time = event_time;
t->event_type = event;
t->next = x->next;
x->next = t;
}
/*************************************************************************
*/
int act() /* find the next event and go to it */
{
int
type;
struct schedule_info
*x;
gmt = head->next->time; /* step time forward to the next event */
type = head->next->event_type; /* Record type of this next event */
x = head->next; /* Delete event from linked list */
head->next = head->next->next;
free(x);
return type; /* return value is type of the next event */
}
/*************************************************************************
/
/*************************************************************************
*/
void sim_init()
/* initialise the simulation */
{
printf("nenter the mean interarrival time and the mean holding
timen");
scanf("%d%d", &iat, &service_time);
printf("enter the total number of customers to be simulatedn");
scanf("%d", &total_events);
printf("enter the seedn");
scanf("%d", &seed);
srand(seed);
printf("enter Number Of Servern");
scanf("%d", &NumberOfServer);
//srand(seed);
head = NEW(EVENTLIST);
tail = NEW(EVENTLIST);
head->next = tail;
tail->next = tail;
//service_time = service_time /10;
q = 0;
narr = 0;
q_sum = 0;
schedule(negexp(iat), ARRIVAL); /* schedule the first arrival */
}
/*************************************************************************
*/
Task 4.1
Take the router queue simulation (r_ssq.c), and modify it so that the simulation measures the
average time spent in the system by packets, i.e. the average time between a packet arriving, and
completion of its service. Assume that the service discipline in first-in-first-out (FIFO).
Use these parameter values:
Mean packet length = 1000 octets (i.e. 8,000 bits) Router capacity = 1 Mbps (i.e. 1,000,000 bps)
Briefly summarize the code changes required to implement the above.
/* program ssq.c */
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <time.h>
#include <limits.h>
#include <iostream>
using namespace std;
#define NEW(type) (type *) malloc(sizeof(type))
#define ARRIVAL 1
#define DEPARTURE 2
/* Event by event simulation of a single server queue with infinite
waiting
room */
double
gmt, /* absolute time */
iat; /* mean interarrival time */
int
q, /* number of customers in the system */
narr, /* number of arrivals */
q_sum, /* sum of queue lengths at arrival instants */
r_capacity, /* router processing capacity */
mean_pkt_length, /* mean packet length */
total_events; /* number of events to be simulated */
double sumOfProcessTime = 0;
int arrivalTime = 0;
int departTime = 0;
int Count=0;
long
seed; /* seed for the random number generator */
typedef struct schedule_info{
double time; /* Time that event occurs */
int event_type; /* Type of event */
struct schedule_info *next; /* Pointer to next item in linked
list */
} EVENTLIST;
struct schedule_info
*head,
*tail;
int act(void);
double negexp(double);
double negexp_srv(int);
void arrival(void);
void departure(void);
void schedule(double, int);
void sim_init(void);
/*************************************************************************
*/
int main(){
sim_init();
while (narr < total_events){
switch (act()){
case ARRIVAL:
arrival();
break;
case DEPARTURE:
departure();
break;
default:
printf("error in act proceduren");
exit(1);
break;
} /* end switch */
} /* end while */
printf(" average time spent in the system by packets is: %8.4fn",
((float) q_sum) / narr);
printf("The answer for task 4.1 is:n");
//Calculating the average time
cout<< (double)(sumOfProcessTime/Count);
int age;
cin >> age;
return(0);
} /* end main */
/*************************************************************************
*/
double negexp(double mean) /* returns a negexp rv with mean `mean' */
{
return ( (int)(- log((float) rand() / RAND_MAX) * mean + 0.5) );
}
/*************************************************************************
*/
double negexp_srv(int mean) /* returns a negexp rv with mean `mean' */
{
int pkt_length;
pkt_length = (int)( (int)(- log((float) rand() / RAND_MAX) * mean + 0.5)
);
double service_time = (pkt_length*8.0)/(r_capacity*pow(10.0,6.0));
//Count number of received packet by router
Count++;
//Calculating the sum of process time for each packet
sumOfProcessTime = (service_time)+sumOfProcessTime;
return (service_time);
}
/*************************************************************************
*/
void arrival() /* a customer arrives */
{
narr += 1; /* keep tally of number of arrivals */
q_sum += q;
schedule(negexp(iat), ARRIVAL); /* schedule the next arrival */
q += 1;
if (q == 1)
{
schedule(negexp_srv(mean_pkt_length), DEPARTURE);
}
//Some of process time, which is the subtrack of arrival time and
departTime calculated in each depart time
//sumOfProcessTime = (arrivalTime - departTime)+sumOfProcessTime;
}
/*************************************************************************
*/
void departure() /* a customer departs */
{
q -= 1;
if (q > 0)
schedule(negexp_srv(mean_pkt_length), DEPARTURE);
}
/*************************************************************************
*/
/*************************************************************************
*/
void schedule(double time_interval, int event) /* Schedules an event of
type */
/* 'event' at time
'time_interval'
in the future */
{
double
event_time;
struct schedule_info
*x,
*t;
event_time = gmt + time_interval;
t = NEW(EVENTLIST);
for(x=head ; x->next->time<event_time && x->next!=tail ; x=x->next);
t->time = event_time;
t->event_type = event;
//if(event == 1)
//{
/// arrivalTime = event_time;
//}
//if(event == 2)
//{
//departTime = event_time;
//}
t->next = x->next;
x->next = t;
}
/*************************************************************************
*/
int act() /* find the next event and go to it */
{
int
type;
struct schedule_info
*x;
gmt = head->next->time; /* step time forward to the next event */
type = head->next->event_type; /* Record type of this next event */
x = head->next; /* Delete event from linked list */
head->next = head->next->next;
free(x);
return type; /* return value is type of the next event */
}
/*************************************************************************
/
/*************************************************************************
*/
void sim_init()
/* initialise the simulation */
{
int iar;
printf("nenter the mean packet arrival rate (pkts/sec) and the mean
packet length (octets)n");
scanf("%d%d", &iar, &mean_pkt_length);
printf("nenter the router transmission capacity (Mbps)n");
scanf("%d", &r_capacity);
printf("enter the total number of packets to be simulatedn");
scanf("%d", &total_events);
// providing automated seed from system time */
seed = time(NULL);
srand(seed);
head = NEW(EVENTLIST);
tail = NEW(EVENTLIST);
head->next = tail;
tail->next = tail;
gmt = 0.0;
q = 0;
narr = 0;
q_sum = 0;
iat = 1.0 / iar;
schedule(negexp(iat), ARRIVAL); /* schedule the first arrival */
}
/*************************************************************************
*/
Task 4.2
Present the results from your simulation of Task 4.1 in graphical form, for occupancies in the range 0.0
to 0.9, including 95% confidence intervals for the estimated mean system times. Show also the
theoretical values on your graph.
Above graph is based on mean system time produced for occupancy between 0.0 to .9 for 100 packets
by 1 Mbps Router and mean packet length of 1000 octet and confidence interval of %95(standard
deviation = .013971)
Mean service time for theoretical values are shown in following graph
occupancy 0.1 0.2 0.3 0.4 0.5 0.6 0.7 0.8 0.9
Mean system time 0.000172 0.000664 0.000744 0.01996 0.02164 0.024264 0.027384 0.032608 0.03496
confidence %95 0.008659 0.008659 0.008659 0.008659 0.008659 0.008659 0.008659 0.008659 0.008659
-0.005
0
0.005
0.01
0.015
0.02
0.025
0.03
0.035
0.04
0 0.2 0.4 0.6 0.8 1
Occupancy
Mean system time
Mean system time
0
0.005
0.01
0.015
0.02
0.025
0 0.01 0.02 0.03 0.04 0.05
Theoritical values
Mean system time
Mean system time
Router Queue Simulation in C++ in MMNN and MM1 conditions

More Related Content

What's hot

Bases de datos Lina Mejia y Paola Varon
Bases de datos Lina Mejia y Paola VaronBases de datos Lina Mejia y Paola Varon
Bases de datos Lina Mejia y Paola Varon
MARCEMEJIA11
 

What's hot (20)

PPT On Sorting And Searching Concepts In Data Structure | In Programming Lang...
PPT On Sorting And Searching Concepts In Data Structure | In Programming Lang...PPT On Sorting And Searching Concepts In Data Structure | In Programming Lang...
PPT On Sorting And Searching Concepts In Data Structure | In Programming Lang...
 
Process Scheduling
Process SchedulingProcess Scheduling
Process Scheduling
 
Chapter 09 design and analysis of algorithms
Chapter 09  design and analysis of algorithmsChapter 09  design and analysis of algorithms
Chapter 09 design and analysis of algorithms
 
Compiler Construction introduction
Compiler Construction introductionCompiler Construction introduction
Compiler Construction introduction
 
Bases de datos Lina Mejia y Paola Varon
Bases de datos Lina Mejia y Paola VaronBases de datos Lina Mejia y Paola Varon
Bases de datos Lina Mejia y Paola Varon
 
Data Structures - Lecture 7 [Linked List]
Data Structures - Lecture 7 [Linked List]Data Structures - Lecture 7 [Linked List]
Data Structures - Lecture 7 [Linked List]
 
Ejercicios de normalizacion
Ejercicios de normalizacionEjercicios de normalizacion
Ejercicios de normalizacion
 
Arreglos unidimensionales
Arreglos unidimensionalesArreglos unidimensionales
Arreglos unidimensionales
 
Bottom up parser
Bottom up parserBottom up parser
Bottom up parser
 
C program language tutorial pattern printing
C program language tutorial pattern printingC program language tutorial pattern printing
C program language tutorial pattern printing
 
Patron Interprete
Patron InterpretePatron Interprete
Patron Interprete
 
Full solution to bounded buffer
Full solution to bounded bufferFull solution to bounded buffer
Full solution to bounded buffer
 
Algorithm Analysis
Algorithm AnalysisAlgorithm Analysis
Algorithm Analysis
 
Solving recurrences
Solving recurrencesSolving recurrences
Solving recurrences
 
Dictionary
DictionaryDictionary
Dictionary
 
The bounded buffer
The bounded bufferThe bounded buffer
The bounded buffer
 
Algorithm And analysis Lecture 03& 04-time complexity.
 Algorithm And analysis Lecture 03& 04-time complexity. Algorithm And analysis Lecture 03& 04-time complexity.
Algorithm And analysis Lecture 03& 04-time complexity.
 
Insertion Sort Algorithm
Insertion Sort AlgorithmInsertion Sort Algorithm
Insertion Sort Algorithm
 
Funções com Ruby[AULA 5]
Funções com Ruby[AULA 5]Funções com Ruby[AULA 5]
Funções com Ruby[AULA 5]
 
Complexity analysis in Algorithms
Complexity analysis in AlgorithmsComplexity analysis in Algorithms
Complexity analysis in Algorithms
 

Viewers also liked

RAMEZ BORAIE PROJECT MANAGEMENTS
RAMEZ BORAIE PROJECT MANAGEMENTSRAMEZ BORAIE PROJECT MANAGEMENTS
RAMEZ BORAIE PROJECT MANAGEMENTS
Ramez Boraie
 
Portafolio / Portfolio
Portafolio / PortfolioPortafolio / Portfolio
Portafolio / Portfolio
Macarena Goles
 

Viewers also liked (20)

MVP 2 & App Demo
MVP 2 & App DemoMVP 2 & App Demo
MVP 2 & App Demo
 
Informatica trabajo1
Informatica trabajo1Informatica trabajo1
Informatica trabajo1
 
Final 441
Final 441Final 441
Final 441
 
Apperture - personas
Apperture - personasApperture - personas
Apperture - personas
 
Escritura
EscrituraEscritura
Escritura
 
Para de transportes
Para de transportesPara de transportes
Para de transportes
 
RAMEZ BORAIE PROJECT MANAGEMENTS
RAMEZ BORAIE PROJECT MANAGEMENTSRAMEZ BORAIE PROJECT MANAGEMENTS
RAMEZ BORAIE PROJECT MANAGEMENTS
 
Encimera AEG HKL65310FB
Encimera AEG HKL65310FBEncimera AEG HKL65310FB
Encimera AEG HKL65310FB
 
Notice web kit
Notice web kitNotice web kit
Notice web kit
 
Preso3
Preso3Preso3
Preso3
 
Formulas
FormulasFormulas
Formulas
 
KoaLinga
KoaLingaKoaLinga
KoaLinga
 
INOCHI_boci 261109
INOCHI_boci 261109INOCHI_boci 261109
INOCHI_boci 261109
 
Presentation inochi réalisations / développement - Accompagnateur Web
Presentation inochi réalisations / développement - Accompagnateur WebPresentation inochi réalisations / développement - Accompagnateur Web
Presentation inochi réalisations / développement - Accompagnateur Web
 
Parcial lauravpdf
Parcial lauravpdfParcial lauravpdf
Parcial lauravpdf
 
Sisältöä elämään!® palvelusetelituottajaksi
Sisältöä elämään!®  palvelusetelituottajaksiSisältöä elämään!®  palvelusetelituottajaksi
Sisältöä elämään!® palvelusetelituottajaksi
 
Mruby jitプレゼン
Mruby jitプレゼンMruby jitプレゼン
Mruby jitプレゼン
 
Portafolio / Portfolio
Portafolio / PortfolioPortafolio / Portfolio
Portafolio / Portfolio
 
Dossier marketing digital dans CCImag de mars 2016
Dossier marketing digital dans CCImag de mars 2016Dossier marketing digital dans CCImag de mars 2016
Dossier marketing digital dans CCImag de mars 2016
 
Customer service dental practice presentation file
Customer service dental practice presentation fileCustomer service dental practice presentation file
Customer service dental practice presentation file
 

Similar to Router Queue Simulation in C++ in MMNN and MM1 conditions

Lab Assignment 4 CSE330 Spring 2014 Skeleton Code for ex.docx
 Lab Assignment 4 CSE330 Spring 2014  Skeleton Code for ex.docx Lab Assignment 4 CSE330 Spring 2014  Skeleton Code for ex.docx
Lab Assignment 4 CSE330 Spring 2014 Skeleton Code for ex.docx
MARRY7
 
Modify this code to use multiple threads with the same data1.Modif.pdf
Modify this code to use multiple threads with the same data1.Modif.pdfModify this code to use multiple threads with the same data1.Modif.pdf
Modify this code to use multiple threads with the same data1.Modif.pdf
mallik3000
 
C aptitude questions
C aptitude questionsC aptitude questions
C aptitude questions
Srikanth
 
C - aptitude3
C - aptitude3C - aptitude3
C - aptitude3
Srikanth
 
Instruction1. Please read the two articles. (Kincheloe part 1 &.docx
Instruction1. Please read the two articles. (Kincheloe part 1 &.docxInstruction1. Please read the two articles. (Kincheloe part 1 &.docx
Instruction1. Please read the two articles. (Kincheloe part 1 &.docx
carliotwaycave
 

Similar to Router Queue Simulation in C++ in MMNN and MM1 conditions (20)

CPP Homework Help
CPP Homework HelpCPP Homework Help
CPP Homework Help
 
Proposed pricing model for cloud computing
Proposed pricing model for cloud computingProposed pricing model for cloud computing
Proposed pricing model for cloud computing
 
Lab Assignment 4 CSE330 Spring 2014 Skeleton Code for ex.docx
 Lab Assignment 4 CSE330 Spring 2014  Skeleton Code for ex.docx Lab Assignment 4 CSE330 Spring 2014  Skeleton Code for ex.docx
Lab Assignment 4 CSE330 Spring 2014 Skeleton Code for ex.docx
 
Ping to Pong
Ping to PongPing to Pong
Ping to Pong
 
Modify this code to use multiple threads with the same data1.Modif.pdf
Modify this code to use multiple threads with the same data1.Modif.pdfModify this code to use multiple threads with the same data1.Modif.pdf
Modify this code to use multiple threads with the same data1.Modif.pdf
 
Ns2 introduction 2
Ns2 introduction 2Ns2 introduction 2
Ns2 introduction 2
 
Introduction to reactive programming & ReactiveCocoa
Introduction to reactive programming & ReactiveCocoaIntroduction to reactive programming & ReactiveCocoa
Introduction to reactive programming & ReactiveCocoa
 
Rpc mechanism
Rpc mechanismRpc mechanism
Rpc mechanism
 
Reference Parameter, Passing object by reference, constant parameter & Defaul...
Reference Parameter, Passing object by reference, constant parameter & Defaul...Reference Parameter, Passing object by reference, constant parameter & Defaul...
Reference Parameter, Passing object by reference, constant parameter & Defaul...
 
C aptitude questions
C aptitude questionsC aptitude questions
C aptitude questions
 
C - aptitude3
C - aptitude3C - aptitude3
C - aptitude3
 
C programming language tutorial
C programming language tutorial C programming language tutorial
C programming language tutorial
 
Lecture no 3
Lecture no 3Lecture no 3
Lecture no 3
 
Fundamental of programming Fundamental of programming
Fundamental of programming Fundamental of programmingFundamental of programming Fundamental of programming
Fundamental of programming Fundamental of programming
 
Functions
FunctionsFunctions
Functions
 
Cassandra 2.1 boot camp, Overview
Cassandra 2.1 boot camp, OverviewCassandra 2.1 boot camp, Overview
Cassandra 2.1 boot camp, Overview
 
Instruction1. Please read the two articles. (Kincheloe part 1 &.docx
Instruction1. Please read the two articles. (Kincheloe part 1 &.docxInstruction1. Please read the two articles. (Kincheloe part 1 &.docx
Instruction1. Please read the two articles. (Kincheloe part 1 &.docx
 
VCE Unit 01 (1).pptx
VCE Unit 01 (1).pptxVCE Unit 01 (1).pptx
VCE Unit 01 (1).pptx
 
~Ns2~
~Ns2~~Ns2~
~Ns2~
 
Kapacitor - Real Time Data Processing Engine
Kapacitor - Real Time Data Processing EngineKapacitor - Real Time Data Processing Engine
Kapacitor - Real Time Data Processing Engine
 

Router Queue Simulation in C++ in MMNN and MM1 conditions

  • 1. Task 1.1 Generate 1000 independent uniformly distributed random numbers (in range 0 to 1). Calculate the sample mean and sample standard deviation of these numbers. #include<math.h> #include <stdio.h> #include <stdlib.h> #include <limits.h> #include<iostream> #include <iostream> #include <string> #include <cstdlib> #include <iomanip> #include <time.h> using namespace std; int main() { float Rand_Value[1000]; srand( (unsigned)time( NULL ) ); for (int i = 0; i < 1000; i++) { Rand_Value[i] = (float) rand()/RAND_MAX ; //cout << (float) rand()/RAND_MAX << endl; } for (int j = 0; j < 1000; j++) { cout<<Rand_Value[j]<<endl; //= (float) rand()/RAND_MAX ; //cout << (float) rand()/RAND_MAX << endl; } cin.get(); return 0; }
  • 2. Task 1.2 Generate 1000 independent exponentially distributed random numbers with mean 35.0. Calculate the sample mean and sample standard deviation of these numbers. -Since I preferred compiling C++ in windows using Visual studio I was not able to use “drand48” to produce exponentially distributed random numbers so I implemented another method using “rand” #include<math.h> #include <stdio.h> #include <stdlib.h> #include <limits.h> #include<iostream> #include<math.h> #include <iostream> #include <string> #include <cstdlib> #include <iomanip> #include <time.h> using namespace std; int main() { const int N = 1000; float Rand_Value_exp[N]; float Rand_Value[N]; float mean = 35.0; float exp,var=0, dev = 0; float sum = 0; srand( (unsigned)time( NULL ) ); for (int i = 0; i < N; i++) {
  • 3. Rand_Value_exp[i] = ( (int)(- log((float) rand() / RAND_MAX) * mean + 0.5) ); //Rand_Value[i] = (float) rand()/ RAND_MAX; sum = sum+Rand_Value_exp[i]; } for (int j = 0; j < N; j++) { cout<<Rand_Value_exp[j]<<endl; //= (float) rand()/RAND_MAX ; } cout<<"Sample mean is equal:n"; exp = sum/N; cout<<sum/N; for(int k=0; k < N;k++) { var = pow((Rand_Value_exp[k] - exp),2) + var; } dev = sqrt(var)/N; cout<<"n"; cout<<"Standard deviation="<<dev; cin.get(); return 0; }
  • 4. Task 2 What conclusions did you draw from Lab 1 concerning: 1. Accuracy vs number of events for a fixed occupancy (or utilisation)? 2. Accuracy vs occupancy for a fixed number of events? In a Single server Queuing modeling in fixed utilization ( where the ratio of arrival rate and departure rate is fixed ) it can be seen that the higher accurate number can be produced when there are less events .it means for example in modeling 1 case where the exact solution is known the accuracy is at its highest. In case of modeling for fixed number of events I have tested the program with 100 events with different arrival rate and holding time ratio and It seems the accuracy is higher for higher utilization.
  • 5. Task 3 Task 1 in Lab 2 asks for a comparison of the mean queue lengths of M/M/1 and M/D/1 queues. What do you conclude? Give some evidence. The difference between M/M/1 and M/D/1 is Deterministic service time which is constant, rather than being exponentially distributed. So we modified the code by eliminating the “negexp” function and compared the results .we observed that the mean queue length for the system with more random case (M/M/1, which has both random arrivals and random service) is greater than the less random case (M/D/1) which can be explained by looking at the formula for: Average length of Queue in M/M/1 system Average length of Queue in M/M/D system Task 3.2 Summarize the code changes required between the M/M/1 ssq.c simulation and the M/M/N/N simulation of Task 2 in Lab 2. In this task the user inputs the numbers of servers to programs ( “NumberOfServer” ) and programs simply multiples the numbers of packets being serviced by this n server.it also decrements the DEPARTURE packets based on n. for example in the case of 3 servers 3 packets will be serviced and once they depart the system 3 packets will be decremented from the list . /* program ssq.c */ #include <stdio.h> #include <stdlib.h> #include <math.h> #include <limits.h> #include <iostream> ( )ρ ρ − = 1 2 Q ( )ρ ρ − = 12 2 Q
  • 6. using namespace std; #define NEW(type) (type *) malloc(sizeof(type)) #define ARRIVAL 1 #define DEPARTURE 2 /* Event by event simulation of a single server queue with infinite waiting room */ int gmt, /* absolute time */ q, /* number of customers in the system */ narr, /* number of arrivals */ q_sum, /* sum of queue lengths at arrival instants */ iat, /* mean interarrival time */ service_time, /* mean holding time */ total_events; /* number of events to be simulated */ int NumberOfServer; /* Number of Server */ unsigned int seed; /* seed for the random number generator */ typedef struct schedule_info{ int time; /* Time that event occurs */ int event_type; /* Type of event */ struct schedule_info *next; /* Pointer to next item in linked list */ } EVENTLIST; struct schedule_info *head, *tail; int act(void); int negexp(int); void arrival(void); void departure(void); void schedule(int, int); void sim_init(void); /************************************************************************* */ int main(){ sim_init(); while (narr < total_events){ switch (act()){ case ARRIVAL: arrival(); break;
  • 7. case DEPARTURE: departure(); break; default: printf("error in act proceduren"); exit(1); break; } /* end switch */ } /* end while */ printf("The mean queue length seen by arriving customers is: %8.4fn", ((float) q_sum) / narr); int n; cin>>n; return(0); } /* end main */ /************************************************************************* */ int negexp(int mean) /* returns a negexp rv with mean `mean' */ { return ( (int)(- log((float) rand() / RAND_MAX) * mean + 0.5) ); } /************************************************************************* */ void arrival() /* a customer arrives */ { //defines number of servers narr += NumberOfServer; /* keep tally of number of arrivals */ q_sum += q; schedule(negexp(iat), ARRIVAL); /* schedule the next arrival */ //accumulates numbers of customers in the system based on number of servers q += NumberOfServer; //marks the last node in the server as DEPARTURE if (q == NumberOfServer) schedule(negexp(service_time), DEPARTURE); } /************************************************************************* */ void departure() /* a customer departs */ { //Decrements number of customers in the system after customer departs q -= NumberOfServer;
  • 8. if (q > 0) schedule(negexp(service_time), DEPARTURE); } /************************************************************************* */ /************************************************************************* */ void schedule(int time_interval, int event) /* Schedules an event of type */ /* 'event' at time 'time_interval' in the future */ { int event_time; struct schedule_info *x, *t; event_time = gmt + time_interval; t = NEW(EVENTLIST); for(x=head ; x->next->time<event_time && x->next!=tail ; x=x->next); t->time = event_time; t->event_type = event; t->next = x->next; x->next = t; } /************************************************************************* */ int act() /* find the next event and go to it */ { int type; struct schedule_info *x; gmt = head->next->time; /* step time forward to the next event */ type = head->next->event_type; /* Record type of this next event */ x = head->next; /* Delete event from linked list */ head->next = head->next->next; free(x); return type; /* return value is type of the next event */ } /************************************************************************* / /************************************************************************* */ void sim_init() /* initialise the simulation */
  • 9. { printf("nenter the mean interarrival time and the mean holding timen"); scanf("%d%d", &iat, &service_time); printf("enter the total number of customers to be simulatedn"); scanf("%d", &total_events); printf("enter the seedn"); scanf("%d", &seed); srand(seed); printf("enter Number Of Servern"); scanf("%d", &NumberOfServer); //srand(seed); head = NEW(EVENTLIST); tail = NEW(EVENTLIST); head->next = tail; tail->next = tail; //service_time = service_time /10; q = 0; narr = 0; q_sum = 0; schedule(negexp(iat), ARRIVAL); /* schedule the first arrival */ } /************************************************************************* */ Task 4.1 Take the router queue simulation (r_ssq.c), and modify it so that the simulation measures the average time spent in the system by packets, i.e. the average time between a packet arriving, and completion of its service. Assume that the service discipline in first-in-first-out (FIFO). Use these parameter values: Mean packet length = 1000 octets (i.e. 8,000 bits) Router capacity = 1 Mbps (i.e. 1,000,000 bps) Briefly summarize the code changes required to implement the above. /* program ssq.c */ #include <stdio.h> #include <stdlib.h> #include <math.h> #include <time.h> #include <limits.h> #include <iostream>
  • 10. using namespace std; #define NEW(type) (type *) malloc(sizeof(type)) #define ARRIVAL 1 #define DEPARTURE 2 /* Event by event simulation of a single server queue with infinite waiting room */ double gmt, /* absolute time */ iat; /* mean interarrival time */ int q, /* number of customers in the system */ narr, /* number of arrivals */ q_sum, /* sum of queue lengths at arrival instants */ r_capacity, /* router processing capacity */ mean_pkt_length, /* mean packet length */ total_events; /* number of events to be simulated */ double sumOfProcessTime = 0; int arrivalTime = 0; int departTime = 0; int Count=0; long seed; /* seed for the random number generator */ typedef struct schedule_info{ double time; /* Time that event occurs */ int event_type; /* Type of event */ struct schedule_info *next; /* Pointer to next item in linked list */ } EVENTLIST; struct schedule_info *head, *tail; int act(void); double negexp(double); double negexp_srv(int); void arrival(void); void departure(void); void schedule(double, int); void sim_init(void); /************************************************************************* */ int main(){
  • 11. sim_init(); while (narr < total_events){ switch (act()){ case ARRIVAL: arrival(); break; case DEPARTURE: departure(); break; default: printf("error in act proceduren"); exit(1); break; } /* end switch */ } /* end while */ printf(" average time spent in the system by packets is: %8.4fn", ((float) q_sum) / narr); printf("The answer for task 4.1 is:n"); //Calculating the average time cout<< (double)(sumOfProcessTime/Count); int age; cin >> age; return(0); } /* end main */ /************************************************************************* */ double negexp(double mean) /* returns a negexp rv with mean `mean' */ { return ( (int)(- log((float) rand() / RAND_MAX) * mean + 0.5) ); } /************************************************************************* */ double negexp_srv(int mean) /* returns a negexp rv with mean `mean' */ { int pkt_length; pkt_length = (int)( (int)(- log((float) rand() / RAND_MAX) * mean + 0.5) ); double service_time = (pkt_length*8.0)/(r_capacity*pow(10.0,6.0)); //Count number of received packet by router Count++; //Calculating the sum of process time for each packet sumOfProcessTime = (service_time)+sumOfProcessTime; return (service_time);
  • 12. } /************************************************************************* */ void arrival() /* a customer arrives */ { narr += 1; /* keep tally of number of arrivals */ q_sum += q; schedule(negexp(iat), ARRIVAL); /* schedule the next arrival */ q += 1; if (q == 1) { schedule(negexp_srv(mean_pkt_length), DEPARTURE); } //Some of process time, which is the subtrack of arrival time and departTime calculated in each depart time //sumOfProcessTime = (arrivalTime - departTime)+sumOfProcessTime; } /************************************************************************* */ void departure() /* a customer departs */ { q -= 1; if (q > 0) schedule(negexp_srv(mean_pkt_length), DEPARTURE); } /************************************************************************* */ /************************************************************************* */ void schedule(double time_interval, int event) /* Schedules an event of type */ /* 'event' at time 'time_interval' in the future */ { double event_time; struct schedule_info
  • 13. *x, *t; event_time = gmt + time_interval; t = NEW(EVENTLIST); for(x=head ; x->next->time<event_time && x->next!=tail ; x=x->next); t->time = event_time; t->event_type = event; //if(event == 1) //{ /// arrivalTime = event_time; //} //if(event == 2) //{ //departTime = event_time; //} t->next = x->next; x->next = t; } /************************************************************************* */ int act() /* find the next event and go to it */ { int type; struct schedule_info *x; gmt = head->next->time; /* step time forward to the next event */ type = head->next->event_type; /* Record type of this next event */ x = head->next; /* Delete event from linked list */ head->next = head->next->next; free(x); return type; /* return value is type of the next event */ } /************************************************************************* / /************************************************************************* */ void sim_init() /* initialise the simulation */ { int iar; printf("nenter the mean packet arrival rate (pkts/sec) and the mean packet length (octets)n"); scanf("%d%d", &iar, &mean_pkt_length); printf("nenter the router transmission capacity (Mbps)n"); scanf("%d", &r_capacity); printf("enter the total number of packets to be simulatedn");
  • 14. scanf("%d", &total_events); // providing automated seed from system time */ seed = time(NULL); srand(seed); head = NEW(EVENTLIST); tail = NEW(EVENTLIST); head->next = tail; tail->next = tail; gmt = 0.0; q = 0; narr = 0; q_sum = 0; iat = 1.0 / iar; schedule(negexp(iat), ARRIVAL); /* schedule the first arrival */ } /************************************************************************* */ Task 4.2 Present the results from your simulation of Task 4.1 in graphical form, for occupancies in the range 0.0 to 0.9, including 95% confidence intervals for the estimated mean system times. Show also the theoretical values on your graph.
  • 15. Above graph is based on mean system time produced for occupancy between 0.0 to .9 for 100 packets by 1 Mbps Router and mean packet length of 1000 octet and confidence interval of %95(standard deviation = .013971) Mean service time for theoretical values are shown in following graph occupancy 0.1 0.2 0.3 0.4 0.5 0.6 0.7 0.8 0.9 Mean system time 0.000172 0.000664 0.000744 0.01996 0.02164 0.024264 0.027384 0.032608 0.03496 confidence %95 0.008659 0.008659 0.008659 0.008659 0.008659 0.008659 0.008659 0.008659 0.008659 -0.005 0 0.005 0.01 0.015 0.02 0.025 0.03 0.035 0.04 0 0.2 0.4 0.6 0.8 1 Occupancy Mean system time Mean system time 0 0.005 0.01 0.015 0.02 0.025 0 0.01 0.02 0.03 0.04 0.05 Theoritical values Mean system time Mean system time