You are on page 1of 84

Computer Simulation and Modelling

PRACTICAL NO : 1 AIM: Single server queuing model. Program:

Computer Simulation and Modelling

Computer Simulation and Modelling

PRACTICAL NO : 2 Title: Double server queuing model. AIM: To simulate a Double sever Queuing Model to generate the n-digit random numbers using Excel. Program:

Computer Simulation and Modelling

PRACTICAL NO : 3 Title: Inventory System Aim: C++ Program/Excel Program to simulate an Inventory System. (The Newspaper Sellers Problem). Program:

Computer Simulation and Modelling

Computer Simulation and Modelling

PRACTICAL NO : 4 Title: Discrete Distribution 4. A: Bernoulli distribution AIM: C++ Program on Bernoulli Distribution. Program:
#include<iostream.h> #include<conio.h> #include<math.h> void main() { double p,q,mean,var,pdf,cdf; clrscr(); cout<<"\n\n\n"; for(int i=1;i<=2;i++) { cout<<"Enter P : "; cin>>p; cout<<endl; q=1-p; cout<<"Mean = "<<p<<endl<<endl; cout<<"Variance = "<<p*q<<endl<<endl; for(int x=0;x<2;x++) { pdf=pow(p,x)*pow(q,1-x); cout<<"P(X) = "<<pdf<<"\t X = "<<x<<endl; } cout<<endl<<endl; } getch(); }

Computer Simulation and Modelling

Output:

Computer Simulation and Modelling

4. B: Binomial distribution AIM:C++ program on binomial distribution Program:


#include<iostream.h> #include<conio.h> #include<math.h> void main() { double n,p,q,mean,var,pmf; double a,b,c; clrscr(); cout<<"\n\n\n"; cout<<"Enter n : "; cin>>n; cout<<endl; cout<<"Enter p : "; cin>>p; cout<<endl; q=1-p; mean=n*p; var=n*p*q; cout<<"Mean = "<<mean<<endl<<endl; cout<<"Variance = "<<var<<endl<<endl; cout<<"X \t\t P(X)"<<endl<<endl; cout<<"------------------------------"<<endl<<endl; for(int x=0;x<=5;x++) { a=b=c=1; for(int i=1;i<=n;i++) { a=a*i; } for(int j=1;j<=x;j++) { b=b*j; } for(int k=1;k<=(n-x);k++) { c=c*k; }

Computer Simulation and Modelling pmf=(a/(b*c))*pow(p,x)*pow(q,(n-x)); cout<<x<<"\t\t"<<pmf<<endl<<endl; } getch(); }

Computer Simulation and Modelling

Output:

10

Computer Simulation and Modelling

4. C: Geometric distribution Aim:C++ program on Geometric distribution Program:


#include<iostream.h> #include<conio.h> #include<math.h> void main() { double p,q,mean,var,pmf; clrscr(); cout<<"\n\n\n"; cout<<"Enter P : "; cin>>p; cout<<endl; q=1-p; mean=1/p; var=q/pow(p,2); cout<<"Mean = "<<mean<<endl<<endl; cout<<"Variance = "<<var<<endl<<endl; cout<<"X \t\t\t PMF"<<endl<<endl; cout<<"------------------------------"<<endl<<endl; for(int x=1;x<=5;x++) { pmf=p*pow(q,x-1); cout<<x<<"\t\t\t"<<pmf<<endl<<endl; } getch(); }

11

Computer Simulation and Modelling

Output:

12

Computer Simulation and Modelling

4. D: Poisson distribution Aim: C++ Program on Poisson Distribution. Program:


#include<iostream.h> #include<conio.h> #include<math.h> void main() { double l,mean,var,pmf; int a=1; clrscr(); cout<<"\n\n\n"; cout<<"Enter l : "; cin>>l; cout<<endl; mean=l; cout<<"Mean = "<<mean<<endl<<endl; cout<<"Variance = "<<l<<endl<<endl; cout<<"X \t\t\t PMF"<<endl<<endl; cout<<"------------------------------------"<<endl<<endl; for(int x=0;x<=4;x++) { for(int i=1;i<=x;i++) { a=a*i; } pmf=exp(-l)*pow(l,x)/a; cout<<x<<"\t\t\t"<<pmf<<endl<<endl; } getch(); }

13

Computer Simulation and Modelling

Output:

Practical No. 5
14

Computer Simulation and Modelling

Title: Continuous Distribution 5. A: Uniform distribution Aim:C++ Program on Uniform Distribution Program:
#include<iostream.h> #include<conio.h> #include<math.h> #include<stdlib.h> void main() { clrscr(); double a,b,pdf,cdf; cout<<"\n\n\nEnter the parameter a: "; cin>>a; cout<<"Enter the parameter b: "; cin>>b; cout<<"\n X"; cout<<"\t P.D.F"; cout<<"\t C.D.F"; cout<<"\n------------------------"; for(int x=a;x<=b;x++) { pdf=1/(b-a); cdf=(x-a)/(b-a); cout<<"\n"<<x; cout<<"\t "<<pdf; cout<<"\t "<<cdf; } getch(); }

Output:

15

Computer Simulation and Modelling

16

Computer Simulation and Modelling

5. B: Exponential distribution Aim:C++ Program on Exponential Distribution Program:


#include<iostream.h> #include<conio.h> #include<stdlib.h> #include<math.h> void main() { clrscr(); double l,pdf,cdf; cout<<"Enter the parameter l :"; cin>>l; cout<<"\n X"; cout<<"\t P.D.F f(x)"; cout<<"\t C.D.F F(x)"; cout<<"\n------------------------------------------"; for(int x=1;x<=10;x++) { pdf=l*exp(-l*x); cdf=1-exp(-l*x); cout<<"\n "<<x; cout<<"\t "<<pdf; cout<<"\t "<<cdf; } getch(); }

17

Computer Simulation and Modelling

Output:

18

Computer Simulation and Modelling

5. C: Normal distribution Aim: C++ Program on Normal Distribution Program:


#include<iostream.h> #include<conio.h> #include<stdlib.h> #include<math.h> void main() { clrscr(); double mu,sig,pdf; double pi=3.14; cout<<"Enter the parameter mu"; cin>>mu; cout<<"\nEnter the parameter sig"; cin>>sig; cout<<"\n\tX "; cout<<"\t P.D.F f(x)"; cout<<"\n------------------------------------"; for(double x=-5;x<=5;x++) { //int a=pow((x-mu),2)/(2*pow(sig,2)); pdf=(1/(sqrt(2*pi)*sig))*exp(-0.5*pow((x-mu)/sig,2)); cout<<"\n\t"<<x; cout<<"\t "<<pdf; } getch(); }

19

Computer Simulation and Modelling

Output:

20

Computer Simulation and Modelling

5. D: Wiebull distribution Aim:C++ Program on Wiebull Distribution Program:


#include<iostream.h> #include<conio.h> #include<stdlib.h> #include<math.h> void main() { clrscr(); double v,a,b,pdf,cdf; cout<<"Enter the parameter v: "; cin>>v; cout<<"Enter the parameter a: "; cin>>a; cout<<"Enter the parameter b : "; cin>>b; cout<<"\n X"; cout<<"\t P.D.F"; cout<<"\t\t C.D.F"; cout<<"\n----------------------------------------------------"; for(int x=1;x<=5;x++) { int c=(x-v)/a; pdf=(b/a)*pow(c,b-1)*exp(-c); cdf=1-exp(-pow(c,b)); cout<<"\n"<<x; cout<<"\t "<<pdf; cout<<"\t "<<cdf; } getch(); }

21

Computer Simulation and Modelling

Output:

22

Computer Simulation and Modelling

5. E: Gamma Distribution Aim:C++ Program on Gamma Distribution Program:


#include<iostream.h> #include<conio.h> #include<stdlib.h> #include<math.h> void main() { clrscr(); double b,t,pdf,cdf,fact=1,fact1=1,sum=0; cout<<"Enter the parameter b :"; cin>>b; cout<<"Enter the parameter t :"; cin>>t; cout<<"\n X"; cout<<"\t P.D.F"; cout<<"\t\t\t C.D.F"; cout<<"\n------------------------------------------------"; // cout<<"\t\t C.D.F"; for(int j=1;j<=b-1;j++) { fact=fact*j; } //cout<<fact; for(int x=1;x<=5;x++) { pdf=((b*t)*pow((b*t*x),b-1)*exp(-b*t*x))/fact; cout<<"\n"<<x; cout<<"\t "<<pdf; for(int i=0;i<=b-1;i++) { for(int k=1;k<=i;k++) { fact1=fact1*k; } sum=sum+(pow((b*t*x),i)*exp(-b*t*x))/fact1; } cdf=1-sum; 23

Computer Simulation and Modelling cout<<"\t\t "<<sum; } getch();

24

Computer Simulation and Modelling

Output:

25

Computer Simulation and Modelling

5.F: Triangular Distribution Aim:To find the probability density function value and cumulative distribution value for continuous Trinagular distribution for the accepted value of a,b and c by the user. Program:
//Program for continuous Triangular distribution. #include<iostream.h> #include<stdio.h> #include<conio.h> #include<math.h> void main() { double vari,mean,c,a,b,pdf,cdf; double x; clrscr(); cout<<Enter The Value of a\n; cin>>a; cout<<"Enter The Value of b\n"; cin>>b; cout<<Enter The Value of c\n; cin>>c; mean=(a+b+c)/3; vari=((a*a)+(b*b)+(c*c)-(a*c)-(a*b)-(b*c))/18; cout<<Mean=<<mean; cout<<variance=<<vari; cout<<The pdf and cdf of triangular distribution is:\n; cout<<X\t\tpdf\t\tcdf\n); for(x=1;x<5.0;x=x+0.5) { if((a<=x)&&(x<=b)) { pdf=(2*(x-a))/((b-a)*(c-a)); if((a<=x)&&(x<b)) { Cdf=((x-a)*(x-a))/((ba)*(c-a)); } Else { Cdf=0.0; } else if((b<=x)&&(x<=c)) 26

Computer Simulation and Modelling { pdf=2*(c-x)/((c-b)*(c-a)); if((b<=x)&&(x<c)) { cdf=1-((c-x)*(c-x)/(cb)*(c-a)); } Else { Cdf=0.0; } } else { pdf=0.0; cdf=0.0; } Cout<<x<<\t\t<<pdf<<\t\t<<cdf; } getch(); }

27

Computer Simulation and Modelling

Output:

28

Computer Simulation and Modelling

5.g Log-Normal Distribution Aim:To find the probability density function value and cumulative distribution value for Continuous LogNormal distribution for the accepted value of and by the user Formula:
Probability Diostribution Function f(x) =(1/ (2)* *x)*e(-(logx- )^2/2 * ) =0 ,0 < x ,otherwise

Mean=e + 2/2 Variance= e 2+ ^2*( e


^2

- 1)

29

Computer Simulation and Modelling

Program:
//program for lognormal distribution with cdf by trapezoidal #include<iostream.h> #include<stdion.h> #include<conio.h> #include<math.h> void main() { double vari, mean, j, pdf, cdf, m, n, x; double z,y; clrscr(); cout<<"Enter the value of mean(m)\n"; cin>>m; cout<<"Enter the value of row(n)\n"; cin>>n; mean=exp(m+(n*n/2)); vari=exp(2*m+(n*n))*(exp(m*m)-1); cout<<"Mean="<<mean<<endl; cout<<"variance="<<vari<<endl; cout<<"The pdf of longnormal distribution is :\n"; cout<<"X\t\tpdf\n"; for(j=1;j<5.0;j=j+1) { z=1/(j*sqrt(2*3.14)*n); y=((log(j)-m)*(log(j)-m))/(2*n*n); pdf=(float)z*exp(-y); cout<<j<<"\t\t"<<pdf; } getch(); }

Output
30

Computer Simulation and Modelling

Practical no 6
31

Computer Simulation and Modelling

Title: Generate Random Numbers. 6. A: Linear Congruential Method Aim: C++ Program to generate Random Numbers using Linear Congruential Method. Description: Write a C/C++/Excel Program to generate Random Numbers
using:Linear Congruential Method. Accept constant multiplier a, increment c, modulus m and seed Xo from the user.

Formula :
Xi+1=(aXi+c)mod m. Xo:seed value or initial value a:Multiplier c:Increment m:Modulus

Program:
32

Computer Simulation and Modelling

#include<iostream.h> #include<conio.h> #include<stdlib.h> #include<math.h> void main() { clrscr(); int a,m,c,x0; double r[]={0,0,0,0,0}; cout<<"Enter the value of a : "; cin>>a; cout<<"Enter the value of c : "; cin>>c; cout<<"Enter the value of m : "; cin>>m; cout<<"Enter the seed value x0 "; cin>>x0; cout<<"\n\ti"<<"\t Xi"<<"\t Ri"<<endl; cout<<"\n---------------------------------"; for(double i=0;i<5;i++) { cout<<"\n\t"<<i; double x=((a*x0)+c)%m; cout<<"\t "<<x; r[i+1]=x/m; x0=x; cout<<"\t "<<r[i+1]; } getch(); }

Output:
33

Computer Simulation and Modelling

6. B: Multiple Congruential Method

34

Computer Simulation and Modelling

Aim: C++ Program to generate Random Numbers using Multiple Congruential Method. Description: Write a C/C++/Excel Program to generate Random Numbers using:
Multiple Congruential Method. Accept constant multiplier a, modulus m and seed Xo from the user Repeat the procedure for new seed value.

Formula :
Xi+1=(aXi)mod m. Xo:seed value or initial value a:Multiplier m:Modulus

Program:

#include<iostream.h> 35

Computer Simulation and Modelling #include<conio.h> #include<stdlib.h> #include<math.h> void main() { clrscr(); int a,m,c=0,x0; double r[]={0,0,0,0,0}; cout<<"Enter the value of a : "; cin>>a; cout<<"Enter the value of m : "; cin>>m; cout<<"Enter the seed value x0 "; cin>>x0; cout<<"\n\ti"<<"\t Xi"<<"\t Ri"<<endl; cout<<"\n---------------------------------"; for(double i=0;i<5;i++) { cout<<"\n\t"<<i; double x=((a*x0)+c)%m; cout<<"\t "<<x; r[i+1]=x/m; x0=x; cout<<"\t "<<r[i+1]; } getch(); }

Output:

36

Computer Simulation and Modelling

6.c.Using Combined Linear Congruential Method Aim: To generate Random Numbers using Combined Linear Conruential Menthod for accepted value of

37

Computer Simulation and Modelling

multiplier a , increment c , modulus m and seed value x0 from the user.

Formula: Xi Ri Ri
=

( (j=1)k(-1)j-1xij) mod m1-1. ,xi>0

=xi/m =m 1-1/m1 ,xi=0

Program:
#include<iostream.h> #include<stdio.h> #include<conio.h> 38

Computer Simulation and Modelling #include<math.h> void main() { int xo,a,c,m,i,rno,cb; double generator; long int calc; double ans,r1,rn; long int array1[10],array2[10],array3[10],array4[10]; int x1; clrscr(); cout<<"\n Enter how many rn you want to combine"; cin>>cb; for(i=0;i<cb;i++) { cout<<"\n Enter the initial value or seed value i.e. xo"; cin>>xo; array1[i]=xo; cout<<"\nEnter the value of constant multiplier i.e. a"; cin>>a; array2[i]=a; cout<<"\nEnter the value of modules value i.e. m"; cin>>m; array3[i]=m; } cout<<"\nx:\n"; for(i=0;i<cb;i++) { cout<<"\t"<<array1[i]; } cout<<"\na:\n"; for(i=0;i<cb;i++) { cout<<"\t"<<array2[i]; } cout<<"\nm:\n"; for(i=0;i<cb;i++) { cout<<"\t"<<array3[i]; } for(i=0;i<cb;i++) { generator=0; calc=array2[i]*array1[i]; generator=calc%array3[i]; array4[i]=generator; } x1=0; 39

Computer Simulation and Modelling for(i=0;i<cb;i++) { if(i%2==0) { x1=x1+array4[i]; } else { x1=x1-array4[i]; } } cout<<"\n-----------------------------\n"; x1=x1%(array3[0]-1); cout<<"x1:"<<x1<<endl; r1=(double)x1/(double)array3[0]; cout<<"Random number:"<<r1<<endl; getch(); }

Output

40

Computer Simulation and Modelling

Practical no 7

Title: Inverse Transform Technique


41

Computer Simulation and Modelling

7. A: Uniform Distribution Aim: C++ Program for generating Random Observation for Uniform Distribution using Inverse Transform Technique Description: Write a C/C++/Excel Program to generate Random Observation for
Uniform Distribution using Inverse Transform Technique using parameters a and b to be entered by the user.

Formula:
c.d.f : F(x)=x-a/b-a=R xi=(b-a)Ri+a

Program:
#include<stdio.h> #include<conio.h> #include<math.h> void main() 42

Computer Simulation and Modelling { float a,b,r[20],x[20]; int n,i; clrscr(); printf("\n\t\t\tINVERSE TRANSFORM TECHNIQUE"); printf("\n\n\t\t\t Uniform Distribution :- "); printf("\n\nEnter no of Xi(s) to be generated : "); scanf("%d",&n); printf("\nEnter the value for a : "); scanf("%f",&a); printf("Enter the value for b : "); scanf("%f",&b); printf("\n\n\NOTE : The values of R's should be > 0 & < 1 \n\n"); for(i=1;i<=n;i++) { printf("Enter R[%d] : ",i); scanf("%f",&r[i]); if ( r[i] < 0 || r[i] > 1) { printf("\nThe value of R[%d] should be > 0 && < 1 ",i); i=i-1; } } for(i=1;i<=n;i++){ x[i]=a+((b-a)*r[i]); } printf("\n\nThe Xi(s) generated are:"); for(i=1;i<=n;i++){ printf("\nx[%d]=%.2f",i,x[i]); } getch(); }

Output:

43

Computer Simulation and Modelling

7. B: Exponential distribution Aim: C++ Program for generating Random Observation for Exponential Distribution using Inverse Transform Technique
44

Computer Simulation and Modelling

Description: Write a C/C++/Excel Program to generate Random Observation for


Exponential Distribution using Inverse Transform Technique using parameters to be entered by the user.

Formula:
c.d.f : F(x)=1-e- x=R xi=(1/ )log(1-Ri)

Program:
#include<stdio.h> #include<conio.h> #include<math.h> void main() 45

Computer Simulation and Modelling { float lamda,x[20],r[20]; int i,n,flag1=1; clrscr(); printf("\n\t\t\tINVERSE TRANSFORM TECHNIQUE"); printf("\n\n\t\t\t Exponential Distribution :- "); printf("\n\nEnter the no of Xi's to be generated : "); scanf("%d",&n); while(flag1==1) { printf("\nEnter the value for lamda (mean number of arrivals per unit time) : "); scanf("%f",&lamda); flag1=0; if( lamda < 0 ) { printf("\nError: lamda (mean nunber of arrivals per unit time) should be greater 0)"); flag1=1; } } for(i=1;i<=n;i++) { printf("Enter R[%d] : ",i); scanf("%f",&r[i]); if ( r[i] < 0 || r[i] > 1) { printf("\nThe value of R[%d] should be > 0 && < 1 \n",i); i=i-1; } } for(i=1;i<=n;i++) { x[i]=-((1/lamda)*log(r[i])); } printf("\n\n The Xi(s) generated are:"); for(i=1;i<=n;i++) { printf("\nx[%d]=%.3f",i,x[i]); } getch(); }

Output:

46

Computer Simulation and Modelling

7. C: Weibull Distribution Aim: Write a C/C++/Excel Program to implement weibull Distribution Using Inverse Transform Technique for a given Distribution

47

Computer Simulation and Modelling

Formula:
c.d.f F(x) =1-e-(x-y/a)^ F(x) =R ,a>0

1-e-(x-y/a)^ = R xi = v + a [-loge(1-Ri)]1/

Program:
#include<stdio.h> #include<conio.h> #include<math.h> 48

Computer Simulation and Modelling void main() { float alpha,beta,x[20],r[20]; int i,n,flag1=1,flag2=1; clrscr(); printf("\t\t\tINVERSE TRANSFORM TECHNIQUE"); printf("\n\n\t\t\t Weibull Distribution :- "); printf("\n\nEnter the no of Xi(s) to be generated : "); scanf("%d",&n); while(flag1==1) { printf("\n\nEnter the value for alpha (scale parameter) : "); scanf("%f",&alpha); flag1=0; if( alpha < 0 ) { printf("\nError: alpha (scale parameter) should be greater 0"); flag1=1; } } while(flag2==1) { printf("\nEnter the value for beta (shape parameter) : "); scanf("%f",&beta); flag2=0; if( alpha < 0 ) { printf("\nError: beta (shape parameter) should be greater 0)"); flag2=1; } } printf("\n\n\NOTE : The values of R's should be > 0 & < 1 \n\n"); for(i=1;i<=n;i++) { printf("Enter R[%d] : ",i); scanf("%f",&r[i]); if ( r[i] < 0 || r[i] > 1) { printf("\nThe value of R[%d] should be > 0 && < 1 \n",i); i=i-1; } } for(i=1;i<=n;i++) 49

Computer Simulation and Modelling { x[i]=alpha*((pow(-log(1-r[i]),(1/beta)))); } printf("\n\nThe Xi(s) generated are:"); for(i=1;i<=n;i++) { printf("\nx[%d]=%.2f",i,x[i]); } getch(); }

Output:

50

Computer Simulation and Modelling

7. D: Triangular Distribution Aim: Write a C/C++/Excel Program to implement triangular Distribution Using Inverse Transform Technique for a given Distribution Formula:
51

Computer Simulation and Modelling p.d.f. f(x) = x , 0<=x<=1 =2-x, 1<=x<=2 =0 , otherwise c.d.f. F(x)=0 , x<=0 2 =x /2 , 0<x<=1 F(x) = R xi = (2Ri)1/2 ,0<x<=1 0<=R<=1/2 1/2 = 2-(2-2Ri) ,1<x<=2 <=R<=1

Program:
#include<stdio.h> #include<conio.h> #include<math.h> void main() { float lamda,x[20],r[20]; int i,n; clrscr(); 52

Computer Simulation and Modelling printf("\n\t\t\tINVERSE TRANSFORM TECHNIQUE"); printf("\n\n\t\t\t Triangular Distribution :- "); printf("\n\nEnter the no of Xi(s) to be generated : "); scanf("%d",&n); printf("\n\n\NOTE : The values of R's should be > 0 & < 1 \n\n"); for(i=1;i<=n;i++) { printf("Enter R[%d] : ",i); scanf("%f",&r[i]); if ( r[i] < 0 || r[i] > 1) { printf("\nThe value of R[%d] should be > 0 && < 1 \n",i); i=i-1; } } for(i=1;i<=n;i++) { if(r[i] >= 0 && r[i] <= 0.5) { x[i]=sqrt(2*r[i]); } if(r[i] > 0.5 && r[i] <= 1) { x[i]=2-sqrt(2*(1-r[i])); } } printf("\n\n The Xi's generated are:"); for(i=1;i<=n;i++) { printf("\n\n\nx[%d]=%.2f",i,x[i]); } getch(); }

Output:

53

Computer Simulation and Modelling

Practical no 8 Title: Random Variate Generation by AcceptanceRejection Technique. Aim: Write a C/C++/Excel Program AcceptanceRejection Technique.
54

Computer Simulation and Modelling

Description: Write a C++ program to find the Random Variate using AcceptanceRejection Method for Poison Distribution with parameter to be entered by user.

Formula:
P(x)=e-x/x! , x=0,1,2,3,....,

Program:
#include<iostream.h> #include<conio.h> #include<stdlib.h> #include<math.h> 55

Computer Simulation and Modelling void main() { double p=1.0,x=0.0,l=0.4,r[5]; double a=exp(-l); cout<<endl<<"A="<<a<<endl<<endl; cout<<"X R.No. P \tDecision Observation"<<endl; cout<<"--- ----- ---- --------- -----------"<<endl; for(int i=0;i<5;i++) { p=p*r[i]; r[i]=0.01*rand()/100; if(p<a){ cout.precision(3); cout<<x<<" "<<r[i]<<" "<<p<<" Accept "<<x<<endl; p=1; } else{ cout.precision(3); cout<<x<<" "<<r[i]<<" "<<p<<" Reject "<<x<<endl; x++; } } getch(); }

OUTPUT:

56

Computer Simulation and Modelling

Practical no 9 Aim: Testing of Random Numbers 1)Frequency Tests. 2)Run Tests.

57

Computer Simulation and Modelling

9. A: Chi-Square Test Description: Write C/C++ Program to test the Random Numbers using: ChiSquare Test. Accept n random numbers, level of significance, number of intervals n, set of observed values and set of estimated values.

Formula:
Chi square calculated : (Oi-Ei)2/Ei

Program:
#include<iostream.h> #include<conio.h> #include<math.h> #include<stdlib.h> #include<malloc.h> class ChiSquareTest { float *r; //for storing random numbers int n,alpha; 58

Computer Simulation and Modelling float c; float chiSq_Cal[10],chiSq_Table,chiSq_Cal_Total; int O[10]; int E; public: void GenerateRandNo(); void ComputeChiSquare(); }; void ChiSquareTest::GenerateRandNo() { cout<<"Enter how many random numbers you want to generate : "; cin>>n; cout<<"\nEnter the value of alpha (1%, 5% or 10%) : "; cin>>alpha; cout<<endl; r=(float *)malloc(sizeof(float) * n); for(int i=0;i<n;i++) { r[i]=(float)(rand()%100) / (float)100; cout<<r[i]<<"\t"; } } void ChiSquareTest::ComputeChiSquare() { float range[]={0.0f,0.1f,0.2f,0.3f,0.4f,0.5f,0.6f,0.7f,0.8f,0.9f,1.0f}; chiSq_Cal_Total=0.0f; E=n/10;

cout<<"\n\n--------------------------------------------------------------------------------\n\n"; cout<<"H0 : Null Hypothesis - Random Numbers are Uniformly distributed"<<endl; cout<<"H1 : Random Numbers are not Uniformly distributed"<<endl<<endl; cout<<"\nRange\t\tO(i)\tE(i)\tChi-Square"<<endl; cout<<"---------------------------------------------"<<endl; for(int m=0;m<10;m++) { O[m]=0; 59

Computer Simulation and Modelling } //Assigning values for(int i=0;i<n;i++) { c=0.1; for(int j=0;j<10;j++) { if(r[i]<c) { O[j]=O[j]+1; break; } else { c=c+0.1; } } } //Printing table for(int k=0;k<10;k++) { cout<<range[k]<<"-"<<range[k+1]; cout<<"\t\t"<<O[k]; cout<<"\t"<<E; chiSq_Cal[i]=(float)pow((O[k]-E),2) / (float)E; cout<<"\t"<<chiSq_Cal[i]; chiSq_Cal_Total=chiSq_Cal_Total+chiSq_Cal[i]; cout<<endl; } //for n=10 degreeoffreedom=9 if(alpha==1) { chiSq_Table=21.7; } else if(alpha==5) { chiSq_Table=16.9; } else { chiSq_Table=14.7; 60

Computer Simulation and Modelling } cout<<"\n\nChi-Square Calculated = "<<chiSq_Cal_Total; cout<<"\nChi-Square Table = "<<chiSq_Table;

if(chiSq_Cal_Total<chiSq_Table) { cout<<"\n\nDecision:- \n\n"; cout<<"\tSince Chi-Square calculated is less than Chi-Square table"; cout<<"\n\tAccept Null hypothesis(H0) at alpha = "<<alpha; cout<<"\n\tAnd Random Numbers are Uniformly Distributed"; } else { cout<<"\n\nDecision:- \n"; cout<<"\tSince Chi-Square calculated is greater than Chi-Square table"; cout<<"\n\tReject Null hypothesis(H0) at alpha = "<<alpha; cout<<"\n\tAnd Random Numbers are Not Uniformly Distributed"; } } void main() { clrscr(); ChiSquareTest obj; obj.GenerateRandNo(); obj.ComputeChiSquare(); getch(); }

Output:

61

Computer Simulation and Modelling

9. B: Kolmogorov - Smirnov Test

62

Computer Simulation and Modelling

Description: Write C/C++ Program to test the Random Numbers using:


Kolmogorov-Smirnov Test. Accept n random numbers, number of observation N, the set of observation (Ri) where I i N.

Formula:
D+=MAX[I/N-R(I)] D-=MAX[R(I)-(I-1)/N] Dcalculated=MAX[D+,D-]

Program:
#include<iostream.h> #include<conio.h> #include<math.h> #include<stdlib.h> 63

Computer Simulation and Modelling #include<malloc.h> class GoodnessOfFit { int n,alpha,T; //T - intervalover which data are collected say 0 to 100 minutes float Dmax,Dmin; float D_Table,D_Cal; float Dplus,Dminus,sum; float *r; float *normalized; float *step2; float *step3; public: void getData(); void CalKolmoSmir(); }; void GoodnessOfFit::getData() { cout<<"\nEnter how many random numbers you want : "; cin>>n; cout<<"\nEnter the value of alpha (1%, 5% or 10%) : "; cin>>alpha; cout<<"\nEnter the interval over which data is collected from 0 to : "; cin>>T; cout<<endl; r=(float *)malloc(sizeof(float) * n); step2=(float *)malloc(sizeof(float) * n); step3=(float *)malloc(sizeof(float) * n); normalized=(float *)malloc(sizeof(float) * n); //Generating random Numbers for(int i=0;i<n;i++) { //r[i]=(float)random(100)/10.0f; cin>>r[i]; //cout<<r[i]<<"\t"; } //Normalizing the inter-arrival times cout<<"\nNormalized inter-arrival times : \n\n"; for(i=0;i<n;i++) { sum=0; for(int j=0;j<=i;j++) { if(i==0) { 64

Computer Simulation and Modelling sum=r[j]; } else { sum=sum+r[j]; } } normalized[i]=sum/(float)T; cout<<normalized[i]<<"\t"; } } void GoodnessOfFit::CalKolmoSmir() { cout<<"\n--------------------------------------------------------------------------------\n\n"; cout<<"H0 : Null Hypothesis - The inter-arrival times are exponentially distributed"<<endl; cout<<"H1 : The inter-arrival times are not exponentially distributed"<<endl<<endl; //Step1 - Arranging in ascending order float temp; for(int i=0;i<n;i++) { for(int j=0;j<n;j++) { if(normalized[i]<normalized[j]) { temp=normalized[j]; normalized[j]=normalized[i]; normalized[i]=temp; } } } //Step2 & 3 - Calculating (i/N)-r[i] and r[i]-((i-1)/N) //cout<<"\n\n\nStep1\tStep2\tStep3"<<endl; //cout<<"-------------------------"<<endl; for(int k=1;k<=n;k++) { //cout<<normalized[k-1]; step2[k-1]=((float)k/(float)n)-normalized[k-1]; //cout<<"\t"<<step2[k-1]; step3[k-1]=normalized[k-1]-((float)(k-1)/(float)n); //cout<<"\t"<<step3[k-1]; //cout<<endl; 65

Computer Simulation and Modelling } Dmax=step2[0]; Dmin=step3[0]; for(int m=1;m<n;m++) { if(Dmax<step2[m]) { Dmax=step2[m]; } if(step3[m]>0) //Discarding negative values { if(Dmin>step3[m]) { Dmin=step3[m]; } } } cout<<"\nDplus = "<<Dmax; cout<<"\nDminus = "<<Dmin; if(Dmax>Dmin) { D_Cal=Dmax; } else { D_Cal=Dmin; } //for n>35 if(alpha==1) { D_Table=1.63/sqrt(n); } else if(alpha==5) { D_Table=1.36/sqrt(n); } else { D_Table=1.22/sqrt(n); } cout<<"\n\nDcalculated = "<<D_Cal; cout<<"\nDtable = "<<D_Table;

66

Computer Simulation and Modelling if(D_Cal<D_Table) { cout<<"\n\nDecision:- \n\n"; cout<<"\tSince Dcalculated is less than Dtable"; cout<<"\n\tAccept Null hypothesis(H0) at alpha = "<<alpha; cout<<"\n\tAnd the inter-arrival times are Exponentially Distributed"; } else { cout<<"\n\nDecision:- \n"; cout<<"\tSince Dcalculated is greater than Dtable"; cout<<"\n\tReject Null hypothesis(H0) at alpha = "<<alpha; cout<<"\n\tAnd the inter-arrival times are Exponentially Distributed"; } } void main() { clrscr(); GoodnessOfFit obj; obj.getData(); obj.CalKolmoSmir(); getch(); }

67

Computer Simulation and Modelling

Output:

68

Computer Simulation and Modelling

9. C: Runs up and Runs Down Test Description: Write C/C++ Program to test the Random Numbers using: Runs up
and Runs Down Test. Accept the total number of runs a to find the mean and variance of a.

Formula:
a=(Mean of runs)=(2N-1)/3 2a=(Variance of runs)=(16n-29)/90 Zcalculated=a-(a/ a)

69

Computer Simulation and Modelling

Program:
#include<iostream.h> #include<conio.h> #include<stdlib.h> #include<math.h> void main() { clrscr(); double a[100],r[100],n,c=0,mu,sig,zcal,t; char b[10]; cout<<"\nEnter the total number of random numbers : "; cin>>n; for(int i=0;i<=9;i++) { a[i]=rand()%100; r[i]=a[i]/100; cout<<"\n "<<r[i]<<"\t\t"; if(r[i]<r[i+1]) { b[i]='+'; } else { b[i]='-'; } cout<<b[i]; } for(int j=0;j<=9;j++) { if(b[j]==b[j+1]) { } else { c++; } } cout<<"\n\nTotal number of runs is: "<<c; mu=((2*n)-1)/3; cout<<"\n\nMean: "<<mu; sig=((16*n)-29)/90; cout<<"\n\nVariance : "<<sig; zcal=(c-mu)/sig;

70

Computer Simulation and Modelling cout<<"\n\nZcal : "<<zcal; cout<<"\n\nEnter Ztable: "; cin>>t; if(zcal<t) { cout<<"\nSince Zcal<Ztable,Accept null hypothesis at alpha=5% and conclude that "<<"\n random numbers are independent"; } else { cout<<"\nSince Zcal>Ztable,Reject null hypothesis at alpha=5% and conclude that "<<"\n random numbers are not independent"; } getch(); }

Output:
71

Computer Simulation and Modelling

9. D: Runs Above & Below Mean Test

72

Computer Simulation and Modelling

Description: Write C/C++ Program to test the Random Numbers using: Runs
Above & Below Mean Test. Accept the number of individual observations above and below the mean as N1 and N2, the total number of runs as b to find the mean and variance for b.

Formula:
X=Add all RNo/N n1=The no.of individual observation above the X. n2=The no.of individual observation below the X. b=(Mean of runs)=((2n1n2))/N+1/2 2b=(Variance of runs)=(2n1n2(2n1n2-N))/N2(N-1) Zcalculated=a-(a/ a)

Program:
#include<iostream.h> #include<conio.h> 73

Computer Simulation and Modelling #include<stdlib.h> #include<math.h> void main() { clrscr(); double a[100],r[100],n,n1=0,n2=0,c=0,mean,mu,sig,zcal,sum=0,t; char b[10]; cout<<"\nEnter the total number of random numbers : "; cin>>n; cout<<"\n"; for(int i=0;i<=9;i++) { a[i]=rand()%100; r[i]=a[i]/100; cout<<" "<<r[i]<<" "; sum=sum+r[i]; } mean=sum/10; cout<<"\n\n"; for(int j=0;j<=9;j++) { if(r[j]>mean) { n1=n1+1; b[j]='+'; } else { n2=n2+1; b[j]='-'; } cout<<" "<<b[j]; } cout<<"\n\nMean of random numbers is : "<<mean; cout<<"\n......................................................"; for(int k=0;k<=9;k++) { if(b[k]==b[k+1]) { } else { c++; } } cout<<"\n\nTotal number of runs is: "<<c; cout<<"\n\nTotal number of runs above mean is: "<<n1; cout<<"\n\nTotal number of runs below mean is: "<<n2; double s; 74

Computer Simulation and Modelling s=2*n1*n2; mu=(s/n)+0.5; cout<<"\n\nMean: "<<mu; sig=(s*(s-n))/(pow(n,2)*(n-1)); cout<<"\n\nVariance : "<<sig; zcal=(c-mu)/sig; cout<<"\n\nZcal : "<<zcal; cout<<"\n\nEnter Ztable: "; cin>>t; if(zcal<t) { cout<<"\nSince Zcal<Ztable,Accept null hypothesis at alpha=5% and conclude that "<<"\n random numbers are independent"; } else { cout<<"\nSince Zcal>Ztable,Reject null hypothesis at alpha=5% and conclude that "<<"\n random numbers are not independent"; } getch(); }

Output:
75

Computer Simulation and Modelling

9. E: Autocorrelation Test

76

Computer Simulation and Modelling

Description: Write a C/C++/Excel Program Autocorrelation Test: Accept N


random numbers, level of significance , the lag m, the starting number i, M such that i+ (M+1)m<=N

Formula:
AutoCorelation =1/(M+1)[k=0M(Ri+km * Ri+(k+1)m)] -0.25

Program:
#include<iostream.h> 77

Computer Simulation and Modelling #include<conio.h> #include<stdlib.h> #include<math.h> void main() { clrscr(); double i,m,N,M,Rho,sig,zcal,sum=0,t; double a[100],r[100]; cout<<"\nEnter the total number of random numbers : "; cin>>N; cout<<"\nEnter the value of i : "; cin>>i; cout<<"\nEnter the value of lag m : "; cin>>m; cout<<"\n"; for(int x=0;x<=29;x++) { a[x]=rand()%100; r[x]=a[x]/100; cout<<" "<<r[x]; } for(int y=i;y<=20;y++) { double R; R=i+((y+1)*m); if(R<=N) { M=y; } } cout<<"\n\nM : "<<M; for(int z=0;z<=M;z++) { sum=sum+(r[i+(z*m)]*r[i+(z+1)*m]); } Rho=(1/(M+1))*sum-0.25; cout<<"\n\nRho : "<<Rho; double l=((13*M)+7); sig=pow(l,0.5)/(12*(M+1)); cout<<"\n\nSigma : "<<sig; zcal=Rho/sig;

cout<<"\n\nZcal : "<<zcal; cout<<"\n\nEnter Ztable: "; cin>>t; if(zcal<t) 78

Computer Simulation and Modelling { cout<<"\nSince Zcal<Ztable,Accept null hypothesis at alpha=5% and conclude that "<<"\n random numbers are independent means they are not autocorrelated"; } else { cout<<"\nSince Zcal>Ztable,Reject null hypothesis at alpha=5% and conclude that "<<"\n random numbers are not independent means they are autocorrelated"; } getch(); }

Output:
79

Computer Simulation and Modelling

Practical no 10

80

Computer Simulation and Modelling

Title:

Pokers Test

Aim: Write a C/C++/Excel Program for Pokers Test.

Program:
#include<iostream.h> #include<stdlib.h> #include<conio.h> #include<math.h> #include<iostream.h> 81

Computer Simulation and Modelling void main() { double a[100],r[100],d; int u[100],t[100],h[100]; double sim[10]={0,0,0,0,0,0,0,0,0,0}; double dif[10]={0,0,0,0,0,0,0,0,0,0}; double chi=0; int n; clrscr(); cout<<"Please Enter many random numbers you want : "; cin>>n; for(int i=0;i<n;i++) { a[i]=rand()%1000; cout<<a[i]<<endl; } double e[100]; for(int j=0;j<n;j++) { h[j]=a[j]/100; e[j]=fmod(a[j],100); t[j]=e[j]/10; u[j]=fmod(a[j],10); } for(int k=1;k<n;k++) { if((h[k]==t[k]) && (t[k]==u[k])) { sim[0]=sim[0]+1; } else if((h[k]==t[k]) || (t[k]==u[k])||(h[k]==u[k])) { sim[1]=sim[1]+1; } else { sim[2]=n-(sim[0]+sim[1]); } } dif[0]=0.01*n; dif[1]=0.27*n; dif[2]=0.72*n; cout<<"--------------------------------------------------------------------\nCombination \tObserved Frequency \tprob \tExpected Frequency\n---------------------------------------------------------------------"<<endl;

82

Computer Simulation and Modelling cout<<"Similar Digits \t\t"<<sim[0]<<"\t\t0.01\t\t"<<dif[0]<<" \n"<<"Like pair Digits\t"<<sim[1]<<"\t"<<"\t0.27\t\t"<<dif[1]<<"\nDifferent digits\t"<<sim[2]<<"\t\t0.72\t\t"<<dif[2]<<endl; for(int l=0;l<=2;l++) { double k=pow((sim[l]-dif[l]),2)/dif[l]; chi=chi+k; } cout<<"\nZCalculated Value = "<<chi; cout<<"\n\nEnter Ztable: "; cin>>d; if(chi<d) { cout<<"\nSince Zcal<Ztable,Accept null hypothesis at alpha=5% and conclude that "<<"\n random numbers are independent"; } else { cout<<"\nSince Zcal>Ztable,Reject null hypothesis at alpha=5% and conclude that "<<"\n random numbers are not independent"; } getch(); }

Output:

83

Computer Simulation and Modelling

84

You might also like