/* xnormal.c */

#include <stdio.h>
#include "random.h"
#include "normal.h"

/*
	test normal_rand() R times, by N trials uniformly filling M=20 bins
	print bin occupancies and report chi-square statistic
*/

double dist[19] = 
{
	-1.645, -1.282, -1.037, -0.842, -0.674, -0.524, -0.385, -0.253, -0.126, 0.0,
	0.126, 0.253, 0.385, 0.524, 0.674, 0.842, 1.037, 1.282, 1.645
};

int main()
{
	const int M=20, N=1000, R=5;
	int i, j, k, a[M];
	double v, y;
	
	usrand (0);
	for (i=0; i<R; ++i) {
		for (j=0; j<M; ++j) a[j] = 0;
		for (j=0; j<N; ++j) {
			y = normal_rand();
			for (k=0; k<M-1; ++k)
				if (y < dist[k]) break;
			++a[k];
		}
		for (j=0; j<M; ++j) printf("%4d", a[j]);
		
		v = 0.0;
		y = N, y /= M;
		for (j=0; j<M; ++j) v += (a[j] - y) * (a[j] - y) / y;
		printf("\n%d degrees of freedom\n", M-1);
		printf("chi-square statistic = %6.2f\n\n", v);
	}
}