Beruflich Dokumente
Kultur Dokumente
Paper I
Section I
M.Sc.(IT)PART-I
CERTIFICATE
This is to certify that Mr. kirtikumar dilip
giripunje
_________________
__________________
Lecture-In-Charge
principal
_________________
__________________
External Examiner
External Examiner
INDEX
Sr.
Date
Pag
No
Topic
e
No.
1.
Single
Sever
Queuing
Model
2.
Two
Sever
Queuing
Model
3.
4.
Discrete Distribution
1.Bernoulli Distribution
2.Binominal
Distribution
3.Geometric
Distribution
4.Poisson Distribution
5.
Continuous Distribution
1. Uniform Distribution
Sign
2. Exponential
Distribution
3. Gamma Distribution
4. Weibull Distribution
5.
Normal Distribution
6. Triangular
Distribution
7. Erlang Distribution
6.
Generate
Random
Number
1. Linear
Congruetial
Method
2. Multi
Congruetial
Method
3. Inverse
Transform
Tech.
4. Accept / Reject Tech.
2. Run Up / Down
3. Run Above / Below
4. Autocorrelation
5. Poker Test
8.
Practical No. 1
Aim- To Implement a Single Server queuing problem
using EXCEL / C
Code:
#include<iostream.h>
#include<conio.h>
#include<stdio.h>
#include<math.h>
#include<stdlib.h>
#include<time.h>
void main()
{
clrscr();
int i,j;
//with this statement diff random no will be generated
every time
time_t t;
srand((unsigned) time(&t));
//inter arrival variables
int arrtime[20],arrrandom[20];
float arrprob[20],cdf=0,arrcdf[20];
//service time variables
int sertime[20],serrandom[20];
float
serprob[]={0,0.05,0.1,0.2,0.3,0.25,0.1};//probability for
service table
float sercdf[20];
//simulation table variables
int rnoarr[20];
int rnoser[20];
int arrivaltime[20],clocktime[20];
int servicetime[20],servicein[20],serviceout[20];
int waittime[20],idletime[20];
float wait=0,idle=0,service=0,timespent=0;
//input arrival time table
for(i=1;i<=8;i++)
{
arrtime[i]=i;
}
//prob for inter arrival table
for(i=1;i<=8;i++)
{
arrprob[i]=0.125;
}
for(i=1;i<=8;i++)
{
cdf=cdf+arrprob[i];
arrcdf[i]=cdf;
}
for(i=1;i<=8;i++)
{
arrrandom[i]=arrcdf[i]*1000;
}
//display input time table//////////////////////////////////
cout<<"Input Arrival time table"<<endl;
cout<<"arrival time:\tprob:\tcdf:\trandom
no.allocation:"<<endl;
for(i=1;i<=8;i++)
{
cout<<arrtime[i]<<"\t\t"<<arrprob[i]<<"\t"<<arrcdf[i]<<"\t\
t";
if(i==1)
{
cout<<"001-"<<arrrandom[i]<<endl;
}
else if(i==8)
{
cout<<arrrandom[i-1]+1<<"-000"<<endl;
}
else
{
cout<<arrrandom[i1]+1<<"-"<<arrrandom[i]<<endl;
}
}
//////////////////////////////////////////////////////////
// input service time table
for(i=1;i<=6;i++)
{
sertime[i]=i;
}
cdf=0;
for(i=1;i<=6;i++)
{
cdf=cdf+serprob[i];
sercdf[i]=cdf;
}
for(i=1;i<=6;i++)
{
serrandom[i]=sercdf[i]*100;
}
//display service time table
cout<<"---------------------------------------------------------------------"<<endl;
cout<<"Input service time table"<<endl;
cout<<"service time:\tprob:\tcdf:\trandom
no.allocation:"<<endl;
for(i=1;i<=6;i++)
{
cout<<sertime[i]<<"\t\t"<<serprob[i]<<"\t"<<sercdf[i]<<"\t
\t";
if(i==1)
{
cout<<"01-"<<serrandom[i]<<endl;
}
else if(i==6)
{
cout<<serrandom[i-1]+1<<"-00"<<endl;
}
else
{
cout<<serrandom[i1]+1<<"-"<<serrandom[i]<<endl;
}
}
cout<<"---------------------------------------------------------------------"<<endl;
/////////////////////////////////////////////////////
//simulation table
//Generating random no for arrival
rnoarr[1]=0;
for(i=2;i<=20;i++)
{
rnoarr[i]=random(1000);
}
for(i=1;i<=20;i++)
{
rnoser[i]=random(100);
}
//classifying arrival random nos
for(i=1;i<=20;i++)
{
for(j=1;j<=8;j++)
{
if(rnoarr[i]<=arrrandom[j])
{
arrivaltime[i]=arrtime[j];
break;
}
}
}
arrivaltime[1]=0;
//classifying service random nos
for(i=1;i<=20;i++)
{
for(j=1;j<=6;j++)
{
if(rnoser[i]<=serrandom[j])
{
servicetime[i]=sertime[j];
break;
}
}
}
//calculating clock time
cdf=0;
for(i=1;i<=20;i++)
{
cdf=cdf+arrivaltime[i];
clocktime[i]=cdf;
}
//calculating service in and out
for(i=1;i<=20;i++)
{
if(i==1)
{
servicein[i]=clocktime[i];
serviceout[i]=servicetime[i];
}
else
{
if(clocktime[i] > serviceout[i-1])
{
servicein[i]=clocktime[i];
serviceout[i]=servicein[i]
+servicetime[i];
}
else
{
servicein[i]=serviceout[i-1];
serviceout[i]=servicein[i]
+servicetime[i];
}
}
}
wait=wait+waittime[i];
service=service+servicetime[i];
}
idle=idle/20;
wait=wait/20;
service=service/20;
timespent=wait+service;
cout<<endl<<"Average waiting time is:
"<<wait<<"mins"<<endl;
cout<<"Average service time is :
"<<service<<"mins"<<endl;
cout<<"Probability of idle server is: "<<idle<<endl;
cout<<"Average time the customer spends in the
system: "<<timespent<<"mins"<<endl;
getch();
}
Output:-
Practical No. 2
Aim: To Implement a two sever system using EXCEL /
C
Code:
#include<iostream.h>
#include <stdlib.h>
#include<conio.h>
#include<stdio.h>
#include<math.h>
#define SIZE 15
void main()
{
//initialization
int i,j,k,a,b;
int cn,atime,stime,temp,rnum;
float time], ssrand[SIZE] ], bfiveA[SIZE],
bfiveB[SIZE];
float ccn[SIZE], aatime[SIZE], sstime[SIZE],
arand[SIZE], aarand[SIZE], srand[SIZE;
float second[SIZE], third[SIZE], fourA[SIZE],
fourB[SIZE], afiveA[SIZE], afiveB[SIZE;
float aprob[SIZE], acdf[SIZE], sprob[SIZE],
scdf[SIZE];
float astime, asstime[SIZE], asprob[SIZE],
ascdf[SIZE];
float bstime, bsstime[SIZE], bsprob[SIZE],
bscdf[SIZE];
for(j=0;j<atime;j++)
{
arand[j]=acdf[j]*1000;
}
//able service();
cout<<"Enter total sevice time for able(e.g 3) \n";
cin>>astime;
cout<<"Enter "<<astime<<" service time for able(e.g
1,1.5.....) \n";
for(i=0;i<astime;i++)
{
cin>>asstime[i];
}
cout<<"Enter probability for "<<astime<<" servises by
able \n";
for(i=0;i<astime;i++)
{
cin>>asprob[i];
}
ascdf[-1]=0;
for(j=0;j<astime;j++)
{
ascdf[j]=ascdf[j-1]+asprob[j];
}
for(j=0;j<astime;j++)
{
asrand[j]=ascdf[j]*100;
}
//beaker service();
cout<<"Enter total sevice time for beaker(e.g 3) \n";
cin>>bstime;
cout<<"Enter "<<bstime<<" service time for able(e.g
1,1.5.....) \n";
for(i=0;i<bstime;i++)
{
cin>>bsstime[i];
}
cout<<"Enter probability for "<<bstime<<" servises
by able \n";
for(i=0;i<bstime;i++)
{
cin>>bsprob[i];
}
bscdf[-1]=0;
for(j=0;j<bstime;j++)
{
bscdf[j]=bscdf[j-1]+bsprob[j];
}
for(j=0;j<bstime;j++)
{
bsrand[j]=bscdf[j]*100;
}
//printing
cout<<"\n Arrival Time \n";
cout<<"----------------------------------------------------------------------\n";
cout<<"Arrival_Time Prob Cumu.Prob
Rand_No_Allocation\n";
cout<<"-----------------------------------------------------------------------\n";
arand[-1]=0;
asrand[-1]=0;
bsrand[-1]=0;
for(j=0;j<atime;j++)
{
cout<<aatime[j]<<"\t\t"<<aprob[j]<<"\t"<<acdf[j
]<<"\t"<<arand[j-1]+1<<"-" <<arand[j]<<"\n";
}
cout<<"\n Service Time for Able \n";
cout<<"-----------------------------------------------------------------------\n";
cout<<"Service_Time Prob Cumu.Prob
Rand_No_Allocation\n";
cout<<"-----------------------------------------------------------------------\n";
for(i=0;i<astime;i++)
{
cout<<asstime[i]<<"\t\t"<<asprob[i]<<"\t"<<ascd
f[i]<<"\t"<<asrand[i-1]+1<<"-"
<<asrand[i]<<"\n";
}
cout<<"\n Service Time for Beaker \n";
cout<<"----------------------------------------------------------------------\n";
cout<<"Service_Time Prob Cumu.Prob
Rand_No_Allocation\n";
cout<<"-----------------------------------------------------------------------\n";
for(i=0;i<bstime;i++)
{
cout<<bsstime[i]<<"\t\t"<<bsprob[i]<<"\t"<<bsc
df[i]<<"\t"<<bsrand[i-1]+1<<"-"
<<bsrand[i]<<"\n";
}
//rand num generation
a=0,b=0;
while(a<cn || b<cn)
{
rnum=rand();
if(a<cn)
{
if(rnum<1000)
{
if(a==0)
aarand[a]=0;
else
aarand[a]=rnum;
a++;
}
}
if(b<cn)
{
if(rnum<100)
{
ssrand[b]=rnum;
b++;
}
}
}
cout<<"\n";
cout<<"---------------------------------\n";
cout<<"ARand \t SRand \n";
cout<<"---------------------------------\n";
for(i=0;i<cn;i++)
{
cout<<aarand[i]<<"\t"<<ssrand[i]<<"\n";
}
//big table
afiveB[-1]=0;
for(i=0;i<cn;i++)
{
if(i==0)
{
second[i]=0;
third[i]=0;
afiveA[i]=0;
bfiveA[i]=0;
}
else
{
arand[-1]=1;
//val for 2nd
for(j=0;j<atime;j++)
{
if(aarand[j]==0)
second[i]=aatime[atime-1];
if(aarand[i]>arand[j-1] &&
aarand[i]<=arand[j])
{
second[i]=aatime[j];
j=atime;
}
}
//val for third
third[i]=third[i-1]+second[i];
//val for afiveA or bfiveA
if(third[i]>=afiveB[i-1])
{
if(afiveB[i-1]>third[i])
{
afiveA[i]=afiveB[i-1];
}
else
{
afiveA[i]=third[i];
}
bfiveA[i]=0;
bfiveB[i]=0;
}
else if(third[i]>bfiveB[i-1])
{
if(bfiveB[i-1]>third[i])
{
bfiveA[i]=bfiveB[i-1];
}
else
{
bfiveA[i]=third[i];
}
afiveA[i]=0;
afiveB[i]=0;
}
}
srand[-1]=1;
if(afiveB[i-1]<=third[i] || i==0)
{
for(k=0;k<astime;k++)
{
if(ssrand[k]==0)
fourA[i]=asstime[astime-1];
if(ssrand[i]>asrand[k-1] &&
ssrand[i]<=asrand[k])
{
fourA[i]=asstime[k];
fourB[i]=0;
break;
}
}
}
else
{
for(k=0;k<bstime;k++)
{
if(ssrand[k]==0)
fourB[i]=bsstime[bstime-1];
if(ssrand[i]>bsrand[k-1] &&
ssrand[i]<=bsrand[k])
{
fourB[i]=bsstime[k];
fourA[i]=0;
break;
}
}
}
//val for afiveB or bfiveB
if(third[i]>=afiveB[i-1])
{
afiveB[i]=fourA[i]+afiveA[i];
bfiveB[i]=0;
}
else if(third[i]>bfiveB[i-1])
{
bfiveB[i]=fourB[i]+bfiveA[i];
afiveB[i]=0;
}
afiveB[-1]=0;
}
cout<<"\n Final Table is \n";
cout<<"----------------------------------------------------------------------------------------------------\n";
cout<<"
Able
Beaker\n";
cout<<"
------------------------------- ------------------------------\n";
cout<<" Number
Time
Clock Service
Service Service Service Service Service\n";
cout<<" of
Between Time of Time
Time
Time Time
Time
Time\n";
cout<<"Customer Arrivals Arrivals
IN
OUT
IN
OUT\n";
cout<<"----------------------------------------------------------------------------------------------------\n";
for(k=0;k<cn;k++)
{
cout<<" "<<ccn[k]<<"
"<<second[k]<<"
"<<third[k]<<"
"<<fourA[k]<<" "
<<afiveA[k]<<"
"<<afiveB[k]<<"
"<<fourB[k]<<"
"<<bfiveA[k]<<"
<<bfiveB[k]<<"\n\n";
}
getch();
}
Output:
"
Practical No. 3
Aim: To Implement a Newspaper seller problem.
Code:
#include<iostream.h>
#include <stdlib.h>
#include<conio.h>
#include<stdio.h>
#include<math.h>
#define SIZE 15
void main()
{
int i, j, k, a, b, rnum, avgdemand, value=0;
{
if(i==0)
cdf[i]=pg;
else if(i==1)
cdf[i]=cdf[i-1]+pf;
else if(i==2)
{
cdf[i]=cdf[i-1]+pp;
value=cdf[i];
}
}
cout<<"\n";
if(value==1)
{
cout<<"Enter Purchase Price of news paper in
cent(e.g 40)\t";
cin>>price;
cout<<"\nEnter Saling Price for news papers in
cent (e.g 40)\t";
cin>>salecost;
cout<<"\n Enter cost for salvage in cent (e.g
40)\t";
cin>>salvagecost;
cout<<"\n";
for(i=0;i<3;i++)
{
trand[i]=cdf[i]*100;
}
grand[i]=good[i]*100;
fair[i]=fair[i]+fair[i-1];
frand[i]=fair[i]*100;
poor[i]=poor[i]+poor[i-1];
prand[i]=poor[i]*100;
}
//big table
//random num for demand & newspaper
a=0,b=0;
while(a<day || b<day)
{
rnum=rand();
if(rnum<100)
{
if(a<day)
{
drand[a]=rnum;//rand num for
demand
a++;
}
else
{
nrand[b]=rnum;//rand num for
newspaper
b++;
}
}
}
cout<<"\n\n";
}
}
}
else if(ttype[j]==2) //fair
{
for(i=1;i<tdemand;i++)
{
frand[tdemand]=1;
if(frand[i-1]<=drand[j] &&
drand[j] <frand[i])
{
ttdemand[j]=demand[i];
}
}
}
else if(ttype[j]==3) //poor
{
prand[tdemand]=1;
for(i=1;i<=tdemand;i++)
{
if(prand[i-1]<=drand[j] &&
drand[j] <prand[i])
{
ttdemand[j]=demand[i];
}
}
}
//1 doller = 100 cent
//revenue loss salvage
if(ttdemand[j]<=avgdemand)
{
revenue[j]=(ttdemand[j]*salecost)/100; //cost in doller
lost[j]=0;
salvage[j]=((avgdemandttdemand[j])*salvagecost)/100;
}
else
{
revenue[j]=(avgdemand*salecost)/100; //cost in doller
lost[j]=(price*(ttdemand[j]avgdemand))/100;
salvage[j]=0;
}
//profit
profit[j]=revenue[j]-lost[j]+salvage[j];
}//j close
//print demand table
grand[-1]=0;
frand[-1]=0;
prand[-1]=0;
cout<<"------------------------------------------------------------------------------------------\n";
cout<<"------------------------------------------------------------------------------------------\n";
for(k=0;k<tdemand;k++)
{
cout<<demand[k]<<"\t"<<good[k]<<"\t"<<g
rand[k-1]+1<<"-"<<grand[k]
<<"\t"<<fair[k]<<"\t"<<frand[k1]+1<<"-"<<frand[k]<<"\t"<<poor[k]
<<"\t"<<prand[k1]+1<<"-"<<prand[k]<<"\n\n";
}
cout<<"\n Final Table is \n";
cout<<"-------------------------------------------------------------------------------------------\n";
cout<<"Days Random Type of Demand
Demand Revenue Lost Salvage Profit \n";
cout<<"
No
Day Random
of sale
\n";
cout<<"-------------------------------------------------------------------------------------------\n";
for(k=0;k<day;k++)
{
cout<<"
"<<days[k]<<"\t"<<nrand[k]<<"\t"<<ttype[k
]<<"\t"<<drand[k]
<<"\t"<<ttdemand[k]<<"\t"<<revenue[k]<<"
\t"<<lost[k]<<"\t"<<salvage[k]<<"\t"<<profi
t[k]<<"\n";
}
}
Else
{
cout<<"Wrong probability values entered\n";
}//if else close
getch();
}
2. Output Values
Practical No. 4
Aim: Simulate the following discrete distribution.
1. Program to find the probability mass function (pmf)
p(X), mean E(X) and variance V(X), for the
Bernoulli destruction. Accept the probability of success
p, the probability of failure q, no. of trails n form the
user and display the value of pmf.
Code:
#include<iostream.h>
#include <stdlib.h>
#include<conio.h>
#include<stdio.h>
#include<math.h>
void main()
{
//initialization
int x,i,n;
float p,q,pdf,mean,variance;
float temp;
clrscr();
getch();
}
Output:-
}
//mean
mean=n*p;
variance=n*p*q;
cout<<"mean="<<mean<<"\n";
cout<<"variance="<<variance<<"\n";
}
else
{
cout<<"value entered for p is not between 0 to
1\n";
}
getch();
}
Output:-
clrscr();
cout<<"Enter value of p(decimal value 0< p < 1)\n";
cin>>p;
if (p>0 && p<1)
{
q=1-p;
//cout<<"p="<<p<<"\t"<<" and q="<<q<<"\n";
cout<<"enter num of trials x (x>=0)\n";
cin>>n;
cout<<"enter value for lam(+ value)\n";
cin>>lam;
cout<<"----------------------\n";
cout<<"x \t pdf \n";
cout<<"----------------------\n";
for(x=0;x<n;x++)
{
for(i=1;i<=x;i++)
{
a=a*i;
}
pdf=(exp(-lam)*pow(lam,x))/a;
cout<<x<<"\t"<<pdf<<"\n\n";
}
mean=lam;
vari=lam;
cout<<"mean="<<mean<<"\n";
cout<<"variance="<<vari<<"\n";
}
else
{
cout<<"wrong value entered\n";
}
getch();
}
Output:-
#include <stdlib.h>
#include<conio.h>
#include<stdio.h>
#include<math.h>
void main()
{
float p,q,x,pdf,mean,vari;
int n,i;
clrscr();
cout<<"Enter value of p(decimal value 0< p < 1)\n";
cin>>p;
if (p>0 && p<1)
{
q=1-p;
cout<<"enter num of trials\n";
cin>>n;
cout<<"---------------------\n";
cout<<"x \t pdf \n";
cout<<"---------------------\n";
for(x=1;x<=n;x++)
{
pdf=(p*pow(q,x-1));
cout<<x<<"\t"<<pdf<<"\n\n";
}
mean=(1/p);
vari=(q/pow(p,2));
cout<<"\n";
cout<<"mean="<<mean<<"\n";
cout<<"variance="<<vari<<"\n";
}
else
{
cout<<"wrong value entered\n";
}
getch();
}
Output:-
Practical No. 5
Aim: Simulate the following continuous distribution
1. Program to find probability distribution function
(PDF) and cumulative distribution function (CDF) for
Uniform distribution accept parameters a and b from
the user for the random variable X which is distributed
uniformly between a and b.
Code:
#include<iostream.h>
#include <stdlib.h>
#include<conio.h>
#include<stdio.h>
#include<math.h>
void main()
{
float pdf,cdf,a,b;
int n,i,x;
clrscr();
cout<<"Enter value of a(p>0)\n";
cin>>a;
cout<<"Enter value of b(q>0)\n";
cin>>b;
pdf=(1/(b-a));
cout<<"--------------------------------\n";
cout<<"x \t pdf \t\t cdf \n";
cout<<"--------------------------------\n";
for(x=a;x<=b;x++)
{
cdf=((x-a)/(b-a));
cout<<x<<"\t"<<pdf<<"\t"<<cdf<<"\n\n";
}
getch();
}
Output:-
cin>>x;
cout<<"--------------------------------\n";
cout<<"x \t pdf \t\t cdf \n";
cout<<"--------------------------------\n";
for(i=0;i<=x;i++)
{
a=exp(i*-lam);
b=lam;
//cout<<"a="<<a<<"\t"; cout<<"b="<<b<<"\n";
pdf=(b*a);
cdf=1-(exp(-i*lam));
cout<<x<<"\t"<<pdf<<"\t"<<cdf<<"\n";
}
getch();
}
Output:-
Code :
#include<iostream.h>
#include<math.h>
#include<conio.h>
int facto(int be)
{
int i,prod=1;
for(i=be;i>=1;i--)
{
prod=prod*i;
}
return prod;
}
void main()
{
float b,t,pdf,cdf,var,mean,sum=0.0;
clrscr();
cout<<"Enter value of Beta: ";
cin>>b;
cout<<"Enter value of theta: ";
cin>>t;
mean=(float)(1/t);
var=(float)(1/(b*t));
int b1=b-1;
cout<<"-----------------------------------------";
cout<<endl<<"x\t pdf\t\tcdf"<<endl;
cout<<"-----------------------------------------"<<endl;
for(int i=1;i<10;i++)
{
float x,y;
x=(b*t)*pow(b*t*i,b-1);
y=exp(-1*b*t*i);
int f=facto(b1);
pdf=(x*y)/f;
for(int j=0;j<=b-1;j++)
{
int f1=facto(j);
sum=sum+(exp(-b*t*i)*pow(b*t*i,j)/f1);
}
cdf=1-sum;
cout<<i<<"\t"<<pdf<<"\t"<<cdf<<endl;
}
getch();
}
Output:-
#include<math.h>
#include<conio.h>
int facto(int x)
{
int i,prod=1;
x=x-1;
for(i=x;i>=1;i--)
{
prod=prod*i;
}
return prod;
}
void main()
{
float b,a,v,temp,temp1,x;
float pdf,cdf,var,mean;
clrscr();
cout<<"Enter value of Beta: ";
cin>>b;
cout<<"Enter value of alpha: ";
cin>>a;
cout<<"Enter value of v: ";
cin>>v;
float b1,b2,b3,b4;
b1=(1/b)+1;
b2=facto(b1);
mean=v+(a*b2);
b3=facto((2/b)+1);
b4=b3-(pow(b2,2));
var=a*a*b4;
cout<<"\nMEAN= "<<mean<<endl;
cout<<"VARIANCE= "<<var<<endl;
cout<<endl<<"X
f(x)
F(x)"<<endl;
for(x=0.0;x<=1.0;x=x+0.2)
{
// start of pdf
if(x>=v)
{
temp1=b/a*(pow((x-v)/a,b-1));
pdf= temp1*exp(-pow((x-v)/a,b));
}
else
pdf=0;+
//end of pdf
//start of cdf
if(x<v)
cdf=0;
else
{
temp=-pow(((x-v)/a),b);
cdf=1-exp(temp);
}
//end of cdf
cout<<"\n"<<x<<"
"<<cdf;
}
getch();
}
"<<pdf<<"
Output:-
cout<<"--------------------------------\n";
cout<<"x \t pdf \n";
cout<<"--------------------------------\n";
for(i=0;i<range;i++)
{
a=pow(x[i]-mue,2);
b=2*pow(rove,2);
c=a=exp(-(a/b));
d=((1/sqrt(2*3.14))*rove);
pdf[i]=d*c;
}
//print
for(i=0;i<range;i++)
{
cout<<x[i]<<"\t"<<pdf[i]<<"\n\n";
}
getch();
}
Output:-
Output:-
Practical No.6
Aim: To generate random numbers and random
variates
{
//initialization
int i;
int num,a,c,x0,m,x[SIZE];
float r[SIZE],xr[SIZE];
clrscr();
cout<<"
Method
Linear Congruential
\n";
cout<<"-------------------------------------------------------\n";
cout<<"How many Random numbers you want to
generate???\n";
cin>>num;
cout<<"Enter seed value Xo(Xo>0)\n";
cin>>x0;
cout<<"Enter value of multiplier a(a>0)\n";
cin>>a;
cout<<"Enter value of increment c (c>0)\n";
cin>>c;
cout<<"Enter value of modulus(remainder) m
(m>0) \n";
cin>>m;
x[0]=x0;
for(i=1;i<=num;i++)
{
x[i]=(((a*x[i-1])+c)%m);
xr[i]=x[i];
r[i]=xr[i]/m;
}
//print
cout<<"\n----------------------------------\n";
cout<<"No\t x[i]\t Rand num\n";
cout<<"-------------------------------------\n";
for(i=1;i<=num;i++)
{
cout<<"x"<<i<<"\t"<<x[i]<<"\t"<<r[i]<<"\n";
}
getch();
}
Output:-
#include<math.h>
#define SIZE 20
void main()
{
//initialization
int i;
int num,a,x0,m,x[SIZE];
float r[SIZE],xr[SIZE];
clrscr();
cout<<"
\n";
cout<<"-------------------------------------------------------\n";
cout<<"How many Random numbers you want to
generate???\n";
cin>>num;
cout<<"Enter seed value Xo(Xo>0)\n";
cin>>x0;
cout<<"Enter value of multiplier a(a>0)\n";
cin>>a;
cout<<"Enter value of modulus(remainder) m
(m>0) \n";
cin>>m;
x[0]=x0;
for(i=1;i<=num;i++)
{
x[i]=((a*x[i-1])%m);
xr[i]=x[i];
r[i]=xr[i]/m;
}
//print
cout<<"\n----------------------------------------------------\n";
cout<<"No\t x[i]\t rand num\n";
cout<<"----------------------------------------------------\n";
for(i=1;i<=num;i++)
{
cout<<"x"<<i<<"\t"<<x[i]<<"\t"<<r[i]<<"\n";
}
getch();
}
Output:-
cout<<"ri\txi"<<endl;
for(i=1;i<=n;i++)
{
r[i]=random(1000);
r[i]=r[i]/1000;
a=(1/lambda)*(-1);
b=log(1-r[i]);
x[i]=a*b;
cout<<r[i]<<"\t"<<x[i]<<endl;
}
getch();
}
Output:-
B. Uniform Distribution
F(x) = x-a / b-a = R
Code:
#include<iostream.h>
x = a + (b-a) R
#include<conio.h>
#include<stdio.h>
#include<stdlib.h>
#include<time.h>
void main()
{
clrscr();
time_t t;
srand((unsigned) time(&t));
int n,i;
float a,b,x[100],r[100];
cout<<"Enter the total random nos you want to
generate: "<<endl;
cin>>n;
cout<<"Enter the value of a and b: "<<endl;
cin>>a;
cin>>b;
cout<<"ri\txi"<<endl;
for(i=1;i<=n;i++)
{
r[i]=random(100);
r[i]=r[i]/100;
x[i]=(b-a)*r[i]+a;
cout<<r[i]<<"\t"<<x[i]<<endl;
}
getch();
Output:-
srand((unsigned) time(&t));
int i=1,x,n=100;
float p,r,lambda,e;
cout<<"Enter the value of lambda: "<<endl;
cin>>lambda;
cout<<"x\trand no.\tp\tdecision\trand obs"<<endl;
e=exp(-lambda);
x=0;
p=1;
r=(float)random(n)/n;
p=p*r;
cout<<x<<"\t"<<r<<"\t\t"<<p<<"\t accept \t
x="<<x<<endl;
while(i<=10)
{
if(p<e)
{
p=1;
r=(float)random(n)/n;
p=p*r;
cout<<x<<"\t"<<r<<"\t\t"<<p<<"\t accept \t
x="<<x<<endl;
}
else
{
r=(float)random(n)/n;
x++;
p=p*r;
cout<<x<<"\t"<<r<<"\t\t"<<p<<"\t reject \t
x="<<x<<endl;
}
i++;
}
getch();
}
Output:-
Practical No. 7
Testing of Random Number
1. Aim: To test the uniformity of the random numbers
using frequency test.
Code:
#include<iostream.h>
#include<conio.h>
#include<math.h>
#include<stdlib.h>
void main()
{
clrscr();
int i,n,j,e=10,x[101],o[11];
float
r[101],chical,chitable,z,y,range[]={0.0,0.1,0.2,0.3,0.4,0.5,0.
6,0.7,0.8,0.9,1.0};
double c,alpha;
cout<<"Enter the totalrandom numbers you want to
generate: "<<endl;
cin>>n;
cout<<"----------------------------------------------------------"<<
endl;
//generating random nos
for(i=1;i<=n;i++)
{
r[i]=random(100);
r[i]=r[i]/100;
}
//display random nos
for(i=1;i<=n;i++)
{
cout<<r[i]<<"\t";
}
cout<<"----------------------------------------------------------"<<
endl;
clrscr();
//accepting the value of alpha
cout<<"Enter the value of alpha: 1% 5% 10% (with 9
degrees of freedom)"<<endl;
cin>>alpha;
if(alpha==1)
chitable=21.7;
else if(alpha==5)
chitable=16.9;
else
chitable=14.7;
cout<<"-------------------------------------------------------------------------------"<<endl;
for(j=1;j<=10;j++)
{
o[j]=0;
}
//classify them in the range
for(i=1;i<=n;i++)
{
c=0.1;
for(j=1;j<=10;j++)
{
if(r[i] < c)
{
o[j]=o[j]+1;
break;
}
else
c=c+0.1;
}
}
cout<<"H0:Null hypothesis--Random nos are
uniform"<<endl;
cout<<"H1:Alternative hypothesis--Random nos are
not uniform"<<endl<<endl;
cout<<"Range\t\tObserve(oi)\tExpected(ei)"<<endl<<endl;
for(j=1;j<=10;j++)
{
cout<<range[j1]<<"-"<<range[j]<<"\t\t"<<o[j]<<"\t\t"<<e<<endl;
}
for(i=1;i<=10;i++)
{
z=pow((o[i]-e),2);
y=z/e;
chical=chical+y;
}
cout<<endl<<"chi-square(calculated) is =
"<<chical<<endl;
cout<<"chi-square(table) at alpha= "<<alpha<<"% is
= "<<chitable<<endl<<endl;
if(chical<chitable)
{
cout<<"Since chi-square(calculated) < chisquare(table)"<<endl;
cout<<"Decision: We accept H0."<<endl;
cout<<"Conclusion: Random nos are
uniform."<<endl;
}
else
{
cout<<"Since chi-square(calculated) > chisquare(table)"<<endl;
cout<<"Decision: We accept H1."<<endl;
cout<<"Conclusion: Random nos are not
uniform."<<endl;
}
getch();
}
Output:-
Code:
#include<stdio.h>
#include<conio.h>
#include<iostream.h>
#include<stdlib.h>
#include<time.h>
#include<math.h>
void main()
{
clrscr();
float r[21],meu,sigma,s,zcal,ztable;
int n,i,count=0,sign[21];
time_t t;
srand((unsigned) time(&t));
cout<<"Enter the total random nost you want to
generate: ";
cin>>n;
cout<<"-------------------------------------------------------"<<en
dl;
//generating random nos.
for(i=1;i<=n;i++)
{
r[i]=random(100);
r[i]=r[i]/100;
}
for(i=1;i<=n;i++)
{
cout<<r[i]<<"\t";
}
cout<<"-------------------------------------------------------"<<en
dl;
//accepting the value of alpha
cout<<"Enter the value of alpha: 1% 5% 10% (with 9
degrees of freedom)"<<endl;
cin>>alpha;
if(alpha==1)
ztable=21.7;
else if(alpha==5)
ztable=16.9;
else
ztable=14.7;
cout<<"-------------------------------------------------------------------------------"<<endl;
else
sign[i]=0;
}sign[20]=1;
//calculating no. of runs
for(i=2;i<=n;i++)
{
if(sign[i-1]==sign[i])
{}
else
count=count+1;
}
count=count+1;
sign[20]=1;
meu=(2*n-1)/3;
s=(16*n-29)/90;
sigma=sqrt(s);
zcal=(count-meu)/sigma;
//displaying the output
cout<<"H0:Null hypothesis--Random nos are
independent"<<endl;
cout<<"H1:Alternative hypothesis--Random nos are
not independent"<<endl<<endl;
cout<<"-----------------------------------------------"<<endl;
cout<<"Total no. of random nos(N)= "<<20<<endl;
cout<<"Mean no. of runs= "<<sigma<<endl;
cout<<"Total no. of runs= "<<count<<endl;
cout<<"Variance= "<<s<<endl;
cout<<"----------------------------------------------------"<<endl;
cout<<endl<<"z-square(calculated) is =
"<<zcal<<endl;
cout<<"z-square(table) is = "<<ztable<<endl<<endl;
if(zcal<ztable)
{
cout<<"Since zcal< ztable"<<endl<<endl;
cout<<"Decicsion: We accept H0."<<endl;
cout<<"Conclusion: Random nos are
independent."<<endl;
}
else
{
cout<<"Since zcal>ztable "<<endl<<endl;
cout<<"Decision :We acceptH1."<<endl;
cout<<"Conclusion: Random nos are not
independent."<<endl;
}
getch();
}
Output:-
3. Runs above & below mean has the mean & variance
as
b = 2n1n2/N + 1 / 2, 2a =
1)
Z0 = b - b / sqrt(2a)
2n1n2(2n1n2 - N) / N2(N-
Code:
#include<stdio.h>
#include<conio.h>
#include<iostream.h>
#include<stdlib.h>
#include<time.h>
#include<math.h>
void main()
{
clrscr();
float r[21],meu,sigma,s,zcal,ztable,sum=0,x;
int n=20,i,count=0,sign[21],n1=0,n2=0,alpha;
float a,b,c,d;
time_t t;
srand((unsigned) time(&t));
//generating random nos.
for(i=1;i<=n;i++)
{
r[i]=random(100);
r[i]=r[i]/100;
sum=sum+r[i];
}
//display random nos.
for(i=1;i<=n;i++)
{
cout<<r[i]<<"\t";
}
cout<<"------------------------------------------------------------------------"<<endl;
//accepting the value of alpha
cout<<"Enter the value of alpha: 1% 5% 10%
"<<endl;
cin>>alpha;
if(alpha==1)
ztable=2.65;
else if(alpha==5)
ztable=1.96;
else
ztable=1.65;
cout<<"------------------------------------------------------------"
<<endl;
//calculating mean
x=sum/n;
//storing sign as 0(+) and 1(-)
for(i=1;i<=n;i++)
{
if(r[i]>x)
{
sign[i]=0;
n1=n1++;
}
else
{
sign[i]=1;
n2++;
}
}
//calculating no. of runs
for(i=2;i<=20;i++)
{
if(sign[i-1]==sign[i])
{}
else
count=count+1;
}
count=count+1;
meu=(2*n1*n2)/n +0.5;
a=2*n1*n2;
b=2*n1*n2-1;
c=n*n;
d=n-1;
s=(a*b)/(c*d);
sigma=sqrt(s);
zcal=(count-meu)/sigma;
zcal=fabs(zcal);
cout<<"H0:Null hypothesis--Random nos are
independent"<<endl;
getch();
}
Output:-
#include<stdio.h>
#include<conio.h>
#include<iostream.h>
#include<math.h>
#include<stdlib.h>
#include<time.h>
void main()
{
clrscr();
time_t t;
srand((unsigned) time(&t));
float r[51],ztable,sum=0,d=0,e=0,row=0,sigma,zcal;
int i,n,j,lag,M=0,alpha,a=0,b=0,c=0;
cout<<"Enter the total numbers you want to
generate:"<<endl;
cin>>n;
//generating random nos
for(j=1;j<=n;j++)
{
r[j]=random(100);
r[j]=r[j]/100;
}
for(j=1;j<=n;j++)
cout<<r[j]<<"\t";
cout<<"---------------------------------------------------------------"<<endl;
//accepting the values from user
cout<<"Select the value of alpha 1% 5% 10%:"<<endl;
cin>>alpha;
if(alpha==5)
ztable=1.96;
else if(alpha==1)
ztable=2.65;
else
ztable=1.65;
cout<<"Enter the value of i: "<<endl;
cin>>i;
cout<<"Enter the value of Lag "<<endl;
cin>>lag;
//calculating M
while(a<=n)
{
M++;
a=i+lag*(M+1);
}
M=M-1;
cout<<"M="<<M<<" is the largest integer satisfying the
relation"<<endl;
cout<<"--------------------------------------------------------"<<e
ndl;
//calculating row,sigma,zcal
for(j=0;j<=M;j++)
{
b=i+j*lag;
c=i+(j+1)*lag;
d=r[b]*r[c];
sum=sum+d;
}
e=(float)1/(M+1);
row=(e*sum)-0.25;
d=0;
d=13*M+1;
d=sqrt(d);
sigma=d/(12*(M+1));
zcal=row/sigma;
zcal=fabs(zcal);
//displaying the values
cout<<"alpha="<<alpha<<"%"<<endl<<endl;
cout<<"H0: Random nos are independent."<<endl;
cout<<"H1: Random nos are not
independent."<<endl<<endl;
cout<<endl<<"z-square(calculated) is = "<<zcal<<endl;
cout<<"z-square(table) is = "<<ztable<<endl<<endl;
if(zcal<ztable)
{
cout<<"Since zcal < ztable"<<endl;
getch();
}
Output:-
#include<stdlib.h>
#include<time.h>
void main()
{
clrscr();
time_t t;
srand((unsigned) time(&t));
float r[51],ztable,sum=0,d=0,e=0,row=0,sigma,zcal;
int i,n,j,lag,M=0,alpha,a=0,b=0,c=0;
cout<<"Enter the total numbers you want to
generate:"<<endl;
cin>>n;
//generating random nos
for(j=1;j<=n;j++)
{
r[j]=random(100);
r[j]=r[j]/100;
}
for(j=1;j<=n;j++)
cout<<r[j]<<"\t";
cout<<"---------------------------------------------------------------"<<endl;
//accepting the values from user
{
b=i+j*lag;
c=i+(j+1)*lag;
d=r[b]*r[c];
sum=sum+d;
}
e=(float)1/(M+1);
row=(e*sum)-0.25;
d=0;
d=13*M+1;
d=sqrt(d);
sigma=d/(12*(M+1));
zcal=row/sigma;
zcal=fabs(zcal);
//displaying the values
cout<<"alpha="<<alpha<<"%"<<endl<<endl;
cout<<"H0: Random nos are independent."<<endl;
cout<<"H1: Random nos are not
independent."<<endl<<endl;
cout<<endl<<"z-square(calculated) is =
"<<zcal<<endl;
cout<<"z-square(table) is = "<<ztable<<endl<<endl;
if(zcal<ztable)
{
cout<<"Since zcal < ztable"<<endl;
cout<<"Decision: Accept H0."<<endl<<endl;
getch();
}
Output:-
Practical No.8
Aim: To perform goodness of fit test using Kolmogorov
- Smirnov
Code:
#include<iostream.h>
#include <stdlib.h>
#include<conio.h>
#include<stdio.h>
#include<math.h>
void main()
{
clrscr();
int i,j,k=1,alpha,num=10;
double D1[50],D2[50];
double Dcal,s;
double dtable,temp;
double cdf=0,arrival[50],normalize[50];
double r[50];
//generating interarrival times through random
function
for(i=0;i<num;i++)
{
r[i]=random(100);
r[i]=r[i]/10;
}
cout<<"Enter value of alpha(1%, 5%, 10%): ";
cin>>alpha;
cout<<"-------------------------------------------------"<<endl;
s=sqrt(num);
if(alpha==1)
dtable=0.490;
else if(alpha==5)
dtable=0.410;
else
dtable=0.368;
{
normalize[i]=arrival[i]/100;
}
for(i=0;i<num;i++)
{
D1[i]=((k)/num)-normalize[i];
D2[i]=normalize[i]-((k-1)/num);
k++;
}
temp=0.0;
for(i=0;i<num;i++)
{
for(j=i;j<num;j++)
{
if(D1[j]<D1[i])
{
temp=D1[i];
D1[i]=D1[j];
D1[j]=temp;
}
}
}
temp=0.0;
for(i=0;i<num;i++)
{
for(j=i;j<num;j++)
{
if(D2[j]<D2[i])
{
temp=D2[i];
D2[i]=D2[j];
D2[j]=temp;
}
}
}
for(i=0;i<num;i++)
{
if(D1[i]<0 )
{
D1[i]=0;
}
if(D2[i]<0)
{
D2[i]=0;
}
}
cout<<endl;
}
else
{
cout<<"Since dcal> dtable"<<endl<<endl;
cout<<"Decicsion: We accept H1."<<endl;
cout<<"Conclusion: Inter arrival times are not
exponentially distributed"<<endl;
}
getch();
}
Output:-