Sie sind auf Seite 1von 134

Computer Simulation & Modeling

Paper I

Section I

Computer Simulation & Modeling

Computer Simulation & Modeling

Mahatma Gandhi Missions


College of Computer Science & Information Technology
At junction NH4 Sion-Panvel Expressway, Kamothe,
Navi Mumbai-410 209.

M.Sc.(IT)PART-I

CERTIFICATE
This is to certify that Mr. kirtikumar dilip
giripunje

of M.Sc.(IT)Part-I. Roll No. 17 has

completed the course of necessary practicals in


the subject of

Computer Simulation & Modeling

under my supervision during the academic year


2010-2011.

Computer Simulation & Modeling

_________________
__________________
Lecture-In-Charge
principal

_________________
__________________
External Examiner
External Examiner

INDEX

Computer Simulation & Modeling

Sr.

Date

Pag

No

Topic

e
No.

1.

Single

Sever

Queuing

Model
2.

Two

Sever

Queuing

Model
3.

Newspaper Seller Problem

4.

Discrete Distribution
1.Bernoulli Distribution
2.Binominal
Distribution
3.Geometric
Distribution
4.Poisson Distribution

5.

Continuous Distribution
1. Uniform Distribution

Sign

Computer Simulation & Modeling

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.

Computer Simulation & Modeling

Random Number Test


1. Frequency Test

2. Run Up / Down
3. Run Above / Below
4. Autocorrelation
5. Poker Test
8.

Goodness of Fit Test


Kolmogorov-Smirnov

Practical No. 1
Aim- To Implement a Single Server queuing problem
using EXCEL / C

Computer Simulation & Modeling

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];

Computer Simulation & Modeling

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)
{

Computer Simulation & Modeling

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

Computer Simulation & Modeling

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++)

Computer Simulation & Modeling

{
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;
}
}

Computer Simulation & Modeling

}
//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];
}
}
}

Computer Simulation & Modeling

//calculating clock time


for(i=1;i<=20;i++)
{
waittime[i]=servicein[i]-clocktime[i];
}
//calculating idle time
for(i=1;i<=20;i++)
{
if(i==1)
idletime[i]=0;
else
idletime[i]=servicein[i]-serviceout[i-1];
}
cout<<"Simulation Table:"<<endl<<endl;
cout<<"arr\tRandom \tinter\tclock\tRandom
\tService\tService\t\twait\tidle"<<endl;
cout<<"no.\tfor arr\ttime\t time\tfor ser\ttime\tIN
OUT\ttime\ttime"<<endl;
for(i=1;i<=20;i++)
{
cout<<i<<"\t"<<rnoarr[i]<<"\t"<<arrivaltime[i]<<"\t"<<clo
cktime[i]<<"\t";
cout<<rnoser[i]<<"\t"<<servicetime[i]<<"\t"<<servicein[i]
<<"\t";
cout<<serviceout[i]<<"\t"<<waittime[i]<<"\t"<<idletime[i]
<<endl;
}
for(i=1;i<=20;i++)
{
idle=idle+idletime[i];

Computer Simulation & Modeling

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:-

Computer Simulation & Modeling

Computer Simulation & Modeling

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];

Computer Simulation & Modeling

int asrand[SIZE], bsrand[SIZE];


clrscr();
cout<<"Enter Total num of cust\n";
cin>>cn;
for(i=0;i<cn;i++)
{
ccn[i]=i+1;
}
//arrival time
cout<<"Enter total time between arrivals(e.g 3) \n";
cin>>atime;
cout<<"Enter "<<atime<<" time between arrivals(e.g
1,1.5.....) \n";
for(i=0;i<atime;i++)
{
cin>>aatime[i];
}
cout<<"Enter probability for "<<atime<<" arrivals \n";
for(i=0;i<atime;i++)
{
cin>>aprob[i];
}
acdf[-1]=0.0;
for(j=0;j<atime;j++)
{
acdf[j]=acdf[j-1]+aprob[j];
}

Computer Simulation & Modeling

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;
}

Computer Simulation & Modeling

//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";

Computer Simulation & Modeling

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";

Computer Simulation & Modeling

}
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

Computer Simulation & Modeling

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;

Computer Simulation & Modeling

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

Computer Simulation & Modeling

{
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])

Computer Simulation & Modeling

{
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])

Computer Simulation & Modeling

{
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]<<"

Computer Simulation & Modeling

"<<fourB[k]<<"
"<<bfiveA[k]<<"
<<bfiveB[k]<<"\n\n";
}
getch();
}

Output:

"

Computer Simulation & Modeling

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;

Computer Simulation & Modeling

int day, tdemand, type, salecost, salvagecost, price,


salvageprice;
int days[SIZE], demand[SIZE], trand[SIZE],
grand[SIZE], frand[SIZE], prand[SIZE];
int ttype[SIZE], ttdemand[SIZE], revenue[SIZE],
drand[SIZE], nrand[SIZE];
float cdf[SIZE], good[SIZE], poor[SIZE], fair[SIZE];
float lost[SIZE], salvage[SIZE], profit[SIZE];
float pg, pf, pp;
clrscr();
cout<<"Enter Total num of Days\n";
cin>>day;
for(i=0;i<day;i++)
{
days[i]=i+1;
}
//Probability for news paper types
cout<<"Enter probability for news paper type
Good(e.g 0.23) \n";
cin>>pg;
cout<<"Enter probability for news paper type Fair(e.g
0.23) \n";
cin>>pf;
cout<<"Enter probability for news paper type Poor(e.g
0.23) \n";
cin>>pp;
//cumulative prob
for(i=0;i<3;i++)

Computer Simulation & Modeling

{
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;
}

Computer Simulation & Modeling

cout<<"Enter total value for demand of news


papers\n";
cin>>tdemand;
cout<<"Enter "<<tdemand<<" value for demand
of news papers(e.g 40,50)\n";
for(i=0;i<tdemand;i++)
{
cin>>demand[i];
}
cout<<"Enter average demand value for news
papers (e.g 70)\n";
cin>>avgdemand;
//good prob
good[-1]=0; fair[-1]=0;
poor[-1]=0;
cout<<"Enter "<<tdemand<<" demand value for
good news papers (e.g 0.40\n";
for(i=0;i<tdemand;i++)
cin>>good[i];
cout<<"Enter "<<tdemand<<" demand value for
fair news papers (e.g 0.40\n";
for(i=0;i<tdemand;i++)
cin>>fair[i];
cout<<"Enter "<<tdemand<<" demand value for
poor news papers (e.g 0.40\n";
for(i=0;i<tdemand;i++)
cin>>poor[i];
for(i=0;i<tdemand;i++)
{
good[i]=good[i]+good[i-1];

Computer Simulation & Modeling

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";

Computer Simulation & Modeling

cout<<"random num for\n";


cout<<"demand"<<"\t"<<"newspaper"<<"\n";
for(i=0;i<day;i++)
cout<<drand[i]<<"\t"<<nrand[i]<<"\n";
//news paper type for day
cout<<"\n Enter type of news papers for
"<<day<<" days (1: Good, 2: Fair, 3:Poor) \n";
for(i=0;i<day;i++)
{
cin>>type;
if(type==1)
ttype[i]=1;
else if(type==2)
ttype[i]=2;
else if(type==3)
ttype[i]=3;
}
//cal rest fields
for(j=0;j<day;j++)
{
if(ttype[j]==1) //good
{
for(i=1;i<tdemand;i++)
{
if(grand[i-1]<=drand[j] &&
drand[j]<grand[i])
{
ttdemand[j]=demand[i];

Computer Simulation & Modeling

}
}
}
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

Computer Simulation & Modeling

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";

Computer Simulation & Modeling

cout<<"Demand Good Rand No Fair Random


No Poor Random No \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";

Computer Simulation & Modeling

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();
}

Computer Simulation & Modeling

Output:1. Input Values

Computer Simulation & Modeling

2. Output Values

Computer Simulation & Modeling

Computer Simulation & Modeling

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();

Computer Simulation & Modeling

cout<<"Enter fractional value for p ( between 0 to


1)\n";
cin>>p;
if(p>0 && p<1)
{
q=1-p;
cout<<"Enter value for n (n>0 i.e 0,1,2,3,.....)\n";
cin>>n;
cout<<"---------------------------\n";
cout<<"X \t pdf \n";
cout<<"---------------------------\n";
for(i=0;i<n;i++)
{
pdf=(pow(p,i))*(pow(q,1-i));//
cout<<i+1<<"\t"<<pdf<<"\n\n";
mean=pdf;
mean=mean+temp;
temp=0.0;
}
variance=p*q;
cout<<"mean="<<mean<<"\n";
cout<<"variance="<<variance<<"\n";
}
else
{
cout<<"value entered for p is not between 0 to
1\n";
}

Computer Simulation & Modeling

getch();
}

Computer Simulation & Modeling

Output:-

Computer Simulation & Modeling

2. Program to find the probability mass function (pmf)


p(X), mean E(X) and variance V(X), for the
Binomial 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,j,n;
float p,q,pdf,mean,variance;
float temp,r=1,s=1,t=1,c;
clrscr();

Computer Simulation & Modeling

cout<<"Enter fractional value for p ( between 0 to


1)\n";
cin>>p;
if(p>0 && p<1)
{
q=1-p;
//cout<<"p="<<p<<"\t"<<"q="<<q<<"\n";
cout<<"Enter value for n (n>0 i.e 0,1,2,3,.....)\n";
cin>>n;
cout<<"x"<<"\t"<<"pdf"<<"\n";
cout<<"-----------------------------\n";
for(x=0;x<n;x++)
{
//pdf
for(j=1;j<=n;j++) //n!
{
r=r*j;
}
for(j=1;j<=x;j++)
//x!
{
s=s*j;
}
for(j=1;j<=n-x;j++)
//n-x!
{
t=t*j;
}
c=(r/(s*t));
pdf=c*((pow(p,x))*(pow(q,n-x)));
cout<<x<<"\t"<<pdf<<"\n";

Computer Simulation & Modeling

}
//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();
}

Computer Simulation & Modeling

Output:-

Computer Simulation & Modeling

3. Program to find the probability mass function (pmf)


p(X), mean E(X) and variance V(X), for the
Poisson 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>
#define SIZE 15
void main()
{
float p,q,x,lam,a=1,pdf,mean,vari;
int n,i;

Computer Simulation & Modeling

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

Computer Simulation & Modeling

{
cout<<"wrong value entered\n";
}
getch();
}

Computer Simulation & Modeling

Output:-

Computer Simulation & Modeling

4. Program to find the probability mass function (pmf)


p(X), mean E(X) and variance V(X), for the
Geometric 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>

Computer Simulation & Modeling

#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";

Computer Simulation & Modeling

cout<<"mean="<<mean<<"\n";
cout<<"variance="<<vari<<"\n";
}
else
{
cout<<"wrong value entered\n";
}
getch();
}

Computer Simulation & Modeling

Output:-

Computer Simulation & Modeling

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.

Computer Simulation & Modeling

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();
}

Computer Simulation & Modeling

Output:-

Computer Simulation & Modeling

2. Program to find probability distribution function


(PDF) and cumulative distribution function (CDF) for
Exponential 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 cdf,a,b,lam;
int i,x;
double pdf;
clrscr();
cout<<"Enter value of lamda(lamda>0)\n";
cin>>lam;
cout<<"Enter value of x(x>0)\n";

Computer Simulation & Modeling

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:-

Computer Simulation & Modeling

3. Program to find probability distribution function


(PDF) and cumulative distribution function (CDF) for
Exponential distribution accept parameters a and b
from the user for the random variable X which is
distributed uniformly between a and b.

Computer Simulation & Modeling

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<<"-----------------------------------------";

Computer Simulation & Modeling

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();
}

Computer Simulation & Modeling

Output:-

Computer Simulation & Modeling

4. Program to find probability distribution function


(PDF) and cumulative distribution function (CDF) for
Weibull distribution.
Code :
#include<iostream.h>

Computer Simulation & Modeling

#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);

Computer Simulation & Modeling

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);

Computer Simulation & Modeling

cdf=1-exp(temp);
}
//end of cdf
cout<<"\n"<<x<<"
"<<cdf;
}
getch();
}

"<<pdf<<"

Computer Simulation & Modeling

Output:-

Computer Simulation & Modeling

5. Program to find probability distribution function


(PDF) and cumulative distribution function (CDF) for
Normal distribution.
Code :
#include<iostream.h>
#include <stdlib.h>
#include<conio.h>
#include<math.h>
#define SIZE 20
void main()
{
int i;
int rove,mue,range,x[SIZE];
float a,b,c,d,pdf[SIZE];
clrscr();
cout<<"Enter value of rove(rove>0)\n";
cin>>rove;
cout<<"Enter value of mue(mue>0)\n";
cin>>mue;
cout<<"Enter total of range of x ( -ve < x < +ve)\n";
cin>>range;
cout<<"Enter "<<range<<" value for x \n";
for(i=0;i<range;i++)
{
cin>>x[i];
}

Computer Simulation & Modeling

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();
}

Computer Simulation & Modeling

Computer Simulation & Modeling

Output:-

Computer Simulation & Modeling

6. Program to find probability distribution function


(PDF) and cumulative distribution function (CDF) for
Triangular distribution.
Code :
#include<iostream.h>
#include <stdlib.h>
#include<conio.h>
#include<math.h>
#define SIZE 20
void main()
{
int i;
int alpha,beta,range,v;
float a,b,c,d,pdf[SIZE];
float x[SIZE];
clrscr();
cout<<"-------------------------------------------------------\n";
cout<<"
Triangular
\n";
cout<<"-------------------------------------------------------\n";
cout<<"Enter value of alpha(alpha>0)\n";
cin>>alpha;

Computer Simulation & Modeling

cout<<"Enter value of Beta(Beta>0)\n";


cin>>beta;
cout<<"Enter value of location parameter v(v>=0)\n";
cin>>v;
cout<<"Enter total of range of x \n";
cin>>range;
cout<<"Enter "<<range<<" value\n";
for(i=0;i<range;i++)
{
cin>>x[i];
}
//gamma value
for(i=0;i<range;i++)
{
a=(alpha/beta);
b=exp(-((x[i]-v)/alpha));
c=pow(((x[i]-v)/alpha),(beta-1));
pdf[i]=(a*b*c);
}
//print
cout<<"-----------------------\n";
cout<<"x \t f(x)\n";
cout<<"-----------------------\n";
for(i=0;i<range;i++)
{
cout<<x[i]<<"\t"<<pdf[i]<<"\n";
}
getch();

Computer Simulation & Modeling

Computer Simulation & Modeling

Output:-

Computer Simulation & Modeling

Practical No.6
Aim: To generate random numbers and random
variates

1. To generate random numbers using linear


congruential method
Code:
#include<iostream.h>
#include <stdlib.h>
#include<conio.h>
#include<math.h>
#define SIZE 20
void main()

Computer Simulation & Modeling

{
//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];

Computer Simulation & Modeling

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();
}

Computer Simulation & Modeling

Output:-

Computer Simulation & Modeling

2. To generate random numbers using multiplicative


congruential method
Code:
#include<iostream.h>
#include <stdlib.h>
#include<conio.h>

Computer Simulation & Modeling

#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<<"

Multiple Linear Congruential Method

\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);

Computer Simulation & Modeling

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();
}

Computer Simulation & Modeling

Output:-

Computer Simulation & Modeling

Computer Simulation & Modeling

3. To generate random variates using Inverse


Transform Tecchnique
a. Exponential Distribution :
F(x) = 1-e x/
Where x>=0 and find F-1 (u) = - In (1-u)
Code :
#include<iostream.h>
#include<conio.h>
#include<stdio.h>
#include<stdlib.h>
#include<time.h>
#include<math.h>
void main()
{
clrscr();
time_t t;
srand((unsigned) time(&t));
int n,i;
float a,b,x[100],r[100],lambda;
cout<<"Enter the total random nos you want to
generate: "<<endl;
cin>>n;
cout<<"Enter the value of lambda "<<endl;
cin>>lambda;

Computer Simulation & Modeling

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:-

Computer Simulation & Modeling

B. Uniform Distribution
F(x) = x-a / b-a = R
Code:
#include<iostream.h>

x = a + (b-a) R

Computer Simulation & Modeling

#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();

Computer Simulation & Modeling

Output:-

Computer Simulation & Modeling

4. TO generate random variates using AcceptanceRejection Technique


Code:
#include<stdio.h>
#include<conio.h>
#include<iostream.h>
#include<stdio.h>
#include<time.h>
#include<math.h>
#include<stdlib.h>
void main()
{
clrscr();
time_t t;

Computer Simulation & Modeling

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;

Computer Simulation & Modeling

x++;
p=p*r;
cout<<x<<"\t"<<r<<"\t\t"<<p<<"\t reject \t
x="<<x<<endl;
}
i++;
}
getch();
}

Computer Simulation & Modeling

Output:-

Computer Simulation & Modeling

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>

Computer Simulation & Modeling

#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";
}

Computer Simulation & Modeling

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++)
{

Computer Simulation & Modeling

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;

Computer Simulation & Modeling

}
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();
}

Computer Simulation & Modeling

Output:-

Computer Simulation & Modeling

2. Aim: To test the uniformity of the random no. using


Runs Ups & Runs Down as well as Runs Above & Below
Mean
i. Runs Ups & Runs Down have the mean & variance as
a = 2N -1 / 3 , 2a = 16N -29 / 90
Z0 = 1 -a / a

Computer Simulation & Modeling

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;

Computer Simulation & Modeling

}
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;

//storing sign as 0(+) and 1(-)


for(i=1;i<n;i++)
{
if(r[i]>r[i+1])
sign[i]=1;

Computer Simulation & Modeling

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;

Computer Simulation & Modeling

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();
}

Computer Simulation & Modeling

Output:-

Computer Simulation & Modeling

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-

Computer Simulation & Modeling

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";
}

Computer Simulation & Modeling

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++;
}

Computer Simulation & Modeling

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;

Computer Simulation & Modeling

cout<<"H1:Alternative hypothesis--Random nos are


not independent"<<endl<<endl;
cout<<"------------------------------------------------------"<<en
dl;
cout<<"meu= "<<meu<<endl;
cout<<"n1= "<<n1<<endl;
cout<<"n2= "<<n2<<endl;
cout<<"sigma= "<<sigma<<endl<<endl;
cout<<"-------------------------------------------------------"<<en
dl;
cout<<endl<<"z-square(calculated) is =
"<<zcal<<endl;
cout<<"z-square(table) at alpha"<<alpha<<"%is =
"<<ztable<<endl<<endl;
if(zcal<ztable)
{
cout<<"Since zcal < ztable"<<endl;
cout<<"Decision: We accept H0."<<endl;
cout<<"Conclusion: Random nos are
independent."<<endl;
}
else
{
cout<<"Since zcal > ztable"<<endl;
cout<<"Decision: We accept H1."<<endl;
cout<<"Conclusion: Random nos are not
independent."<<endl;
}

Computer Simulation & Modeling

getch();
}

Output:-

Computer Simulation & Modeling

4. Aim: To test the uniformity of the random no. using


Autocorrelation
Code:

Computer Simulation & Modeling

#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";

Computer Simulation & Modeling

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;

Computer Simulation & Modeling

//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;

Computer Simulation & Modeling

cout<<"Decision: Accept H0."<<endl<<endl;


cout<<"Conclusion: Random nos are
independent."<<endl;
}
else
{
cout<<"Since zcal > ztable"<<endl;
cout<<"Decision: Accept H1."<<endl<<endl;
cout<<"Conclusion: Random nos are not
independent."<<endl;
}

getch();
}

Computer Simulation & Modeling

Output:-

Computer Simulation & Modeling

5. Aim: To test uniformity of the random no. using


Pokers test
Code:
#include<stdio.h>
#include<conio.h>
#include<iostream.h>
#include<math.h>

Computer Simulation & Modeling

#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

Computer Simulation & Modeling

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++)

Computer Simulation & Modeling

{
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;

Computer Simulation & Modeling

cout<<"Conclusion: Random nos are


independent."<<endl;
}
else
{
cout<<"Since zcal > ztable"<<endl;
cout<<"Decision: Accept H1."<<endl<<endl;
cout<<"Conclusion: Random nos are not
independent."<<endl;
}

getch();
}

Computer Simulation & Modeling

Output:-

Computer Simulation & Modeling

Practical No.8
Aim: To perform goodness of fit test using Kolmogorov
- Smirnov
Code:

Computer Simulation & Modeling

#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);

Computer Simulation & Modeling

if(alpha==1)
dtable=0.490;
else if(alpha==5)
dtable=0.410;
else
dtable=0.368;

cout<<"Null Hypothesis (H0)-Inter arrival times are


exponentially distributed"<<endl;
cout<<"Alternate Hypothesis(H1)-Inter arrival times
are not exponentially distributed"<<endl;
cout<<"------------------------------------------------------------------------------"<<endl;
//finding arrival time
for(i=0;i<num;i++)
{
arrival[i]=0;
}
for(i=0;i<num;i++)
{
cdf=cdf+r[i];
arrival[i]=cdf;
}
//finding normalized data points
for(i=0;i<num;i++)

Computer Simulation & Modeling

{
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;
}
}
}

Computer Simulation & Modeling

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;

Computer Simulation & Modeling

cout<<"D1 values:\t D2 values:\n";


cout<<"------------------------\n";
for(j=0;j<num;j++)
{
cout<<D1[j]<<"\t\t"<<D2[j]<<endl;
}
cout<<endl;
cout<<"D1 max="<<D1[num-1]<<"\n";
cout<<"D2 max="<<D2[num-1]<<"\n\n";
//find max in D1 & D2
if(D1[num-1]>=D2[num-1]) {
Dcal=D1[num-1]; }
else
{
Dcal=D2[num-1]; }
cout<<"\n";
cout<<"Table value at alph a="<<alpha<<"%
="<<dtable<<"\n";
cout<<"Calculated value ="<<Dcal<<"\n\n";
if(Dcal<dtable)
{
cout<<"Since dcal< dtable"<<endl<<endl;
cout<<"Decicsion: We accept H0."<<endl;
cout<<"Conclusion: Inter arrival times are
exponentially distributed"<<endl;

Computer Simulation & Modeling

}
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:-

Computer Simulation & Modeling

Das könnte Ihnen auch gefallen