Title: Chi-Square test
Objective:
Generate random no using Linear Congruential Generator and perform Chi-square Test on it
Theory:
Linear Congruential Generates random number using 
zi=(azi-1+c) mod m
where,
a=multiplicative factor
Zi-1=initial value
c=addition factor
m=modulus
U=z/m(gives the random number on [0,1])
#Note LCG should have full period.
For chi-square test
 χ 02 =k/n ∑i=0k(Oi-n/k)2
Where k is no. of sub intervals, n is total no. of random variables and Oi is observed no. of variables in the range.
Source Code:
#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
#define maxsize 20
void main()
{
            int z[maxsize],a,m,c,i,o[10];
            float u[maxsize],temp,chi=0.0;
            clrscr();
            printf("\n \t Generation of RANDOM NUMBER using LCG\n");
            printf("\t Enter the value of a:");               //multiplicative factor
            scanf("%d",&a);
            printf("\t Enter the value of c:");              //additive factor
            scanf("%d",&c);
            printf("\t Enter Z0:");                  //initial value
            scanf("%d",&z[0]);
            printf("\t Enter m:"); //modulus
            scanf("%d",&m);
            u[0]=(float)z[0]/m;
            printf("\t 0\t %d\t%f",z[0],u[0]);
            for(i=1;i<m;i++)                       //To generate the random number
            {
                        z[i]=((a*z[i-1]+c)%m);
                        temp=z[i];
                        u[i]=temp/m;
                        printf("\n \t %d\t%d\t%f",i,z[i],u[i]);
            }
            for(i=0;i<10;i++)
                        o[i]=0;
            for(i=0;i<m;i++)
                        {
                                    if(u[i]>=0.0&& u[i]<0.1)
                                    {
                                                o[0]=o[0]+1;
                                    }
                                    else if(u[i]>=0.1&& u[i]<0.2)
                                    {
                                                o[1]=o[1]+1;
                                    }
                                    else if(u[i]>=0.2&& u[i]<0.3)
                                    {
                                                o[2]=o[2]+1;
                                    }
                                    else if(u[i]>=0.3&& u[i]<0.4)
                                    {
                                                o[3]=o[3]+1;
                                    }
                                    else if(u[i]>=0.4&& u[i]<0.5)
                                    {
                                                o[4]=o[5]+1;
                                    }
                                    else if(u[i]>=0.5&& u[i]<0.6)
                                    {
                                                o[5]=o[5]+1;
                                    }
                                    else if(u[i]>=0.6&& u[i]<0.7)
                                    {
                                                o[6]=o[6]+1;
                                    }
                                    else if(u[i]>=0.7&& u[i]<0.8)
                                    {
                                                o[7]=o[7]+1;
                                    }
                                    else if(u[i]>=0.8&& u[i]<0.9)
                                    {
                                                o[8]=o[8]+1;
                                    }
                                    else if(u[i]>=0.9&& u[i]<1)
                                    {
                                                o[9]=o[9]+1;
                                    }
                        }
                        for(i=0;i<10;i++)
                        {
                                    chi=(float)(chi+(o[i]-(m/10))*(o[i]-(m/10)));
                        }
                        chi=(10/(float)m)*chi;
                        printf("\n \t The value if Chi is obtained as: %f",chi);
                        getch();
}
 
 
 
 ^ Go to Top
^ Go to Top
No comments:
Post a Comment