Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                

System Simulation and Modeling Lab

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 23

System Simulation and Modeling Lab

System Simulation and Modeling


(CSX- 424)

PRACTICALS RECORD

DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING


Dr. B R AMBEDKAR NATIONAL INSTITUTE OF TECHNOLOGY
JALANDHAR – 144011, PUNJAB (INDIA)
March 2018

Submitted To: Submitted By:


Dr. Santosh Singh Rathore Kalyani Suman
CSE Department 14103047
Cse Dept

14103047 Page 1
System Simulation and Modeling Lab

Index

S.No. Title Page Date Signature


No.
1. Menu driven program to generate random
numbers using: a) Mid Square Method
b) Residue Method
c)Arithmetic Congruential Method

2. Implementation of Chi Square Method in


Microsoft Excel

3. Implementation of KS Test in Microsoft Excel

4. Implementation of Monte Carlo Method in


Microsoft Excel

5. Menu driven program to generate random variates


using following probability distribution function:
a) Uniform Distribution
b) Normal Distribution
c) Exponential Distribution

6. Implement Single server Queuing System.

7. Menu driven program to implement probability


distribution using geometrical, poisson, triangular
and lognormal distribution

14103047 Page 2
System Simulation and Modeling Lab

Experiment-1
Objective: Write a menu driven program to generate random numbers using

i. Mid Square Method


ii. Residue Method or Linear Congruential Method
iii. Arithmetic Congruential Method

Theory: A random variable or stochastic variable is a variable whose possible values are
numerical outcomes of a random phenomenon.

Mid Square Method:

•  Start with a four-digit positive integer Z0 (seed)

•  Compute the square of Z0 i.e. Z02 =Z0* Z0, to obtain an integer with up to eight digits

•  Take the middle four digits for the next four-digit number

Linear Congruential Method:

To produce a sequence of integers, X1, X2, … between 0 and m-1 by following a recursive
relationship: Xi+1=(Xi*a + c) mod m ; i=1,2,3…

• X0 is called the seed

• The selection of the values for a, c, m, and X0 drastically affects the statistical properties and
the cycle length.

Arithmetic Congruential Method:

To produce a sequence of random integers, X1, X2,…between 0 and m-1 by following the
recursive relationship:

Xi+1=(Xi + Xi-1) mod m

Xi+2=(Xi+1 + Xi) mod m; i=1,2,3…

• X0 and X1 are called seeds.

Program:

#include<iostream>
#include<stdlib.h>
using namespace std;
int main()
{

14103047 Page 3
System Simulation and Modeling Lab

int i;
int count=0;
cout<<"\t\tGeneration of random numbers";
while(1)
{
cout<<"\nEnter one of the option :\n1.Mid Square Method\n2.Residue
Method\n3.Arithmetic Congrutial Method\n";
cin>>i;
switch(i)
{
case 1:
int n,rnd,num,skip;
cout<<"Enter any number: ";
cin>>n;
cout<<"Enter the length of series: ";
cin>>rnd;
num=n;
while(num!=0)
{
count++;
num=num/10;
}
skip=count/2;
if(count==1)
skip=1;
while(rnd--)
{
n=n*n;
skip=count/2;
if(count==1)
skip=1;
while(skip--)
{
n=n/10;
}
int x=1;
for(i=0;i<count;i++)
{
x=x*10;
}

14103047 Page 4
System Simulation and Modeling Lab

n=n%x;
cout<<n<<endl;
}
break;
case 2:
int a,c,m,r;
cout<<"Enter upper limit of random nums to be generated: ";
cin>>m;
cout<<"Enter num of random nums to be generated: ";
cin>>n;
cout<<"Enter initial seed: ";
cin>>r;
cout<<"Enter constant a and c: ";
cin>>a>>c;
while(n--)
{
r=(a*r + c) % m;
cout<<r<<" ";
}
cout<<"\n";
break;
case 3:
int r1,r0,M,N,R;
cout<<"Enter upper limit of random nums to be generated: ";
cin>>M;
cout<<"Enter r1 and r0: ";
cin>>r1>>r0;
cout<<"Enter num of random nums to be generated: ";
cin>>N;
while(N--)
{
R= (r1 + r0) % M;
r0= r1;
r1= R;
cout<<R<<" ";
}
cout<<"\n";
break;
default:
exit(0);

14103047 Page 5
System Simulation and Modeling Lab

}
}
}

Output:

14103047 Page 6
System Simulation and Modeling Lab

Experiment -2
Objective: Implement the Chi-Square Test in Microsoft Excel.

Theory:

Chi- Squared Test: A chi-squared test is any statistical hypothesis test where the sampling
distribution of the test statistic is a chi-squared distribution when the null hypothesis is true. This
test id used to determine whether there is a significant difference between the expected
frequencies and the observed frequencies in one or more categories.

H0=Null hypothesis

Expected frequency ei=(row total*Column total)/total

X2cal= ∑ (oi - ei)2 / ei


Degree of freedom = (row-1)* (column-1)

If X2cal > X2table , then reject the hypothesis H0.

14103047 Page 7
System Simulation and Modeling Lab

Experiment -3
Objective: Implement KS Test in Microsoft Excel.

Theory:

KS Test: The Kolmogorov-Smirnov Test or KS Test is a nonparametric test of the equality of


continuous, one-directional probability distributions that can be used to compare a sample with a
reference probability distribution, or to compare two samples. The KS statistic quantifies a
distance between the empirical distribution function of the sample and the cumulative
distribution function of the reference distribution or between the empirical distribution functions
of two samples.

F(x) and G(x) are the cumulative distribution functions.

Dm,n= max|F(x)-G(x)|

Dm,n,α= C(α)* ((m+n)/m*n)1/2

C(α)=(-ln(α/2)/2)1/2 ; α=0.05

If Dm,n> Dm,n,α then reject the hypothesis H0.

14103047 Page 8
System Simulation and Modeling Lab

Experiment -4
Objective: Implement Monte carlo method in Microsoft Excel.

Theory: Monte Carlo simulation performs risk analysis by building models of possible results
by substituting a range of values—a probability distribution—for any factor that has inherent
uncertainty. It then calculates results over and over, each time using a different set of random
values from the probability functions. Depending upon the number of uncertainties and the
ranges specified for them, a Monte Carlo simulation could involve thousands or tens of
thousands of recalculations before it is complete. Monte Carlo simulation produces distributions
of possible outcome values.

1. Dentist Problem

2. Head and Tail Game

14103047 Page 9
System Simulation and Modeling Lab

14103047 Page 10
System Simulation and Modeling Lab

Experiment -5
Objective: Implement a menu driven program to generate random variates using uniform
distribution, normal and exponential distribution.

Theory:

Uniform Distribution

The uniform distribution is the simplest example of a continuous probability distribution is given
by the formula:-

Normal Distribution

Or “bell curve.” The user simply defines the mean or expected value and a standard deviation to
describe the variation about the mean. Values in the middle near the mean are most likely to
occur. It is symmetric and describes many natural phenomena such as people’s heights.

Exponential Distribution

Program:
#include<iostream>
#include<stdlib.h>
#include<cmath>
using namespace std;
int main()
{
int i,n;
float x,y;
cout<<"\tGeneration of random numbers using probability distribution";
while(1)
{
cout<<"\nEnter option:\n\t1.Uniform Distribution\n\t2.Normal
Distribution\n\t3.Exponential Distribution\n\t4.Exit\n";

14103047 Page 11
System Simulation and Modeling Lab

cin>>i;
switch(i)
{
case 1:
int a,b;
cout<<"enter a, b: ";
cin>>a>>b;
cout<<"enter number of random variable to be generated:";
cin>>n;
cout<<"random numbers:\n";
while(n--)
{
float y=((double)rand()/(RAND_MAX));
x=y*(b-a) + a;
cout<<"y= "<<y<<"\tx= "<<x<<"\n";
}
break;
case 2:
int mu,sigma;

cout<<"Enter mu, sigma: ";


cin>>mu>>sigma;
cout<<"enter number of random variable to be generated:";
cin>>n;
cout<<"random numbers:\n";
while(n--)
{
y=((double)rand()/(RAND_MAX)) / 1000;
x=mu + (sigma * sqrt(-2 * log(sigma*y*sqrt(2*3.14))));
cout<<"y= "<<y<<"\tx= "<<x<<"\n";
}
break;
case 3:
int lambda;
cout<<"enter lambda: ";
cin>>lambda;
cout<<"enter number of random variable to be generated:";
cin>>n;
while(n--)
{

14103047 Page 12
System Simulation and Modeling Lab

y=((double)rand()/(RAND_MAX));
x=log(lambda / y) / lambda;
cout<<"y= "<<y<<"\tx= "<<x<<"\n";
}
break;
case 5:
exit(0);
}
}
return 0;
}
Output:

14103047 Page 13
System Simulation and Modeling Lab

14103047 Page 14
System Simulation and Modeling Lab

Experiment -6
Objective: Implement single server queuing system.

Theory:

 Single-sever service node consists of a server plus its queue.


 Queue discipline: The algorithms used when a job is selected from the queue to enter
service are: FIFO, LIFO, SIRO, and Priority.

For a job i:

 The arrival time is ai


 The delay in the queue is di
 The time that service begins is bi = ai + di
 The service time is si
 The wait in the node is wi = di + si
 The departure time is ci = ai + wi
 The interarrival time between jobs i − 1 and i is ri = ai − ai−1 where, by definition, a0 = 0
 Average interarrival time:

 Average service time:

 The average delay and average wait are defined as

Program:

14103047 Page 15
System Simulation and Modeling Lab

#include <iostream>
using namespace std;
int summation(int a[],int n){
int sum=0;
for (int i=0;i<n;i++)
{
sum+=a[i];
}
return sum;
}
int waiting_cust(int a[],int n)
{ int waiters=0;
for(int i=0;i<n;i++){
if(a[i]>0)
waiters++;
}
return waiters;
}
int main()
{
int cust;
cout<<"enter no of customers"<<endl;
cin>>cust;
int iat[cust],st[cust];
int arrt[cust],tsb[cust],tcwq[cust],tse[cust],tcss[cust],its[cust];
for (int i=0;i<cust;i++)
{
cout<<"enter IAT and ST for customer:"<<i+1<<endl;
cin>>iat[i]>>st[i];
arrt[i]=i>0?iat[i]+arrt[i-1]:0;
tsb[i]=i>0? max(arrt[i],tse[i-1]):0;
tcwq[i]=tsb[i]-arrt[i];
tse[i]=st[i]+tsb[i];
tcss[i]=tse[i]-arrt[i];
if(i>0)
{
if(arrt[i]>tse[i-1])
its[i]=arrt[i]-tse[i-1];
else
its[i]=0;

14103047 Page 16
System Simulation and Modeling Lab

}
else
its[i]=0;
}

cout<<"Customer\t"<<"IAT\t"<<"AT\t"<<"ST\t"<<"TSB\t"<<"TCWQ\t"<<"TSE\t"<<"TCSS\t"
<<"ITS\n";
for(int i=0;i<cust;i++)
{

cout<<i<<"\t"<<iat[i]<<"\t"<<arrt[i]<<"\t"<<st[i]<<"\t"<<tsb[i]<<"\t"<<tcwq[i]<<"\t"<<tse[i]<<
"\t"<<tcss[i]<<"\t"<<its[i]<<"\t"<<endl;
}
double avg_wait_time=summation(tcwq,cust)/(double)cust;
double prob_cust_waits=waiting_cust(tcwq,cust)/(double)cust;
double avg_service_time=summation(st,cust)/(double)cust;
double avg_time_btw_arr=summation(iat,cust)/(double)cust-1;
double avg_wait_time_of_waiters=summation(tcwq,cust)/(double)waiting_cust(tcwq,cust);
double avg_time_spent_in_system=summation(tcss,cust)/(double)cust;
double prob_idle_server=summation(its,cust)/(double)summation(tsb,cust);
double utilization_percent=summation(its,cust)*100/(double)summation(tcss,cust);
cout<<"avg_wait_time="<<avg_wait_time;
cout<<"prob_cust_waits="<<prob_cust_waits;
cout<<"avg_service_time="<<avg_service_time;
cout<<" avg_time_btw_arr="<< avg_time_btw_arr;
cout<<"avg_wait_time_of_waiters="<<avg_wait_time_of_waiters;
cout<<"avg_time_spent_in_system="<<avg_time_spent_in_system;
cout<<"prob_idle_server="<<prob_idle_server;
cout<<"utilization_percent="<<utilization_percent;
return 0;
}

Intput:

14103047 Page 17
System Simulation and Modeling Lab

Output:

14103047 Page 18
System Simulation and Modeling Lab

Experiment -7
Objective: Implement a menu driven program to generate random variables using geometrical,
Poisson, triangular and lognormal distribution.

Theory:

Geometrical distribution:

Given a sequence of Bernoulli trials, let X represent the number of trials required until the first
success

Poisson distribution:

 Often used to model arrival processes with constant arrival rates


 Gives (probability of) the number of events that occur in a given period

 Where alpha is the mean arrival rate. Note that alpha must be positive

Triangular distribution:

In probability theory and statistics, the triangular distribution is a continuous probability


distribution with lower limit a, upper limit b and mode c, where a < b and a ≤ c ≤ b. PDF is
given as:

Lognormal distribution:

14103047 Page 19
System Simulation and Modeling Lab

In probability theory, a log-normal (or lognormal) distribution is a continuous probability


distribution of a random variable whose logarithm is normally distributed. Thus, if the random
variable X is log-normally distributed, then Y = ln(X)has a normal distribution. Likewise, if Y has
a normal distribution, then the exponential function of Y, X = exp(Y), has a log-normal
distribution. A random variable which is log-normally distributed takes only positive real values.
PDF is given as:

Program:

#include<iostream>
#include<stdlib.h>
#include<cmath>
using namespace std;
int fact(int n)
{
int f=1;
while(n>=2)
{
f=f*n;
n--;
}
return f;
}
int main()
{
int i;
float p_x;
cout<<"\tDiscrete probability distribution";
while(1)
{
cout<<"\nEnter option:\n\t1.Geometrical Distribution\n\t2.Poisson
Distribution\n\t3.Triangular Distribution\n\t4.Lognormal Distribution\n\t5.Exit\n";
cin>>i;
switch(i)
{
case 1:
float p,q;
cout<<"Enter probability of success: ";

14103047 Page 20
System Simulation and Modeling Lab

cin>>p;
q=1-p;
float x1;
cout<<"Enter num of trials until first success: ";
cin>>x1;
if(x1>0)
{
p_x = pow(q,x1-1) * p;
}
else
p_x=0;
cout<<"Probability of Xth trial to be first success: "<<p_x<<"\n";
break;
case 2:
float al;
int x2;
cout<<"Enter mean arrival rate: ";
cin>>al;
cout<<"Enter num of periods: ";
cin>>x2;
if(x2>=0)
{
p_x = (pow(2.303, -1*al) * pow(al, x2)) / fact(x2);
}
else
p_x=0;
cout<<"Probability of success for X period: "<<p_x<<"\n";
break;
case 3:
float a,b,c;
float x3;
cout<<"Enter a,b,c,x: ";
cin>>a>>b>>c>>x3;
if(x3>=a && x3<b)
{
p_x = (float)(2 * (x3-a)) / ((b-a) * (c-a));
}
else if(x3>=b && x3<c)
{
p_x = (float) (2 * (c-x3)) / ((c-b) * (c-a));

14103047 Page 21
System Simulation and Modeling Lab

}
else
p_x=0;
cout<<"Probability= "<<p_x<<"\n";
break;
case 4:
float sigma,mu,x4;
cout<<"Enter sigma, mu and x: ";
cin>>sigma>>mu>>x4;
if(x4>=0)
{
p_x = (float) sigma * x4 * pow(2.303, (-1) * pow(log(x4)-sigma,2) /
(2*sigma*sigma)) / sqrt(2*3.14);
}
else
p_x=0;
cout<<"Probability= "<<p_x<<"\n";
break;
case 5:
exit(0);
}
}
return 0;
}

Output:

14103047 Page 22
System Simulation and Modeling Lab

14103047 Page 23

You might also like