Listing 3: ChiSqFrq.c
/* ============ */ /* ChiSqFrq.c */ /* ============ */ /* -------------------------------- */ /* WorkArea is defined in freqtst.c */ /* -------------------------------- */ #define Freq WorkArea /* ====================================================== */ /* ChiSqFreq - Calculates Chi-Square Value of Frequencies */ /* ====================================================== */ void ChiSqFreq(CHISQ_STRU * ChiSqData) { ULONG i, MaxNums, MaxSize; double SosFreq = 0; /* --------------------------------------- */ /* RandFun is assumed to produce uniformly */ /* distributed random numbers in [0,32767] */ /* --------------------------------------- */ MaxSize = (ULONG)ChiSqData->NumElements; MaxNums = ChiSqData->NumVariates; for (i = 0; i < MaxSize; ++i) { Freq[i] = 0; } /* --------------------------------------- */ /* Collect Data for Chi-Square Calculation */ /* --------------------------------------- */ for (i = 0; i < MaxNums; ++i) { int NewRand; NewRand = (int)(ChiSqData->RandFun() % (int)MaxSize); ++Freq[NewRand]; } /* --------------------------- */ /* Counting Variates Generated */ /* --------------------------- */ ChiSqData->TotNumGen += MaxNums; /* ---------------------------------------- */ /* Calculate Sums of Squares of Frequencies */ /* ---------------------------------------- */ for (i = 0; i < MaxSize; ++i) { SosFreq += SQR((double) Freq[i]); } /* -------------------------- */ /* Calculate Chi-Square Value */ /* -------------------------- */ ChiSqData->ChiSqValu = (SosFreq / (double) MaxNums) * (double) MaxSize - (double) MaxNums; } /* End of File */