#include <stdio.h>
#include <stdlib.h>
#include <sys/time.h>
#include <unistd.h>
#include <gsl/gsl_math.h>
#include <gsl/gsl_rng.h>

double time1, time2;
struct timeval starttime,endtime;

short unsigned int random_numbers[1000000];

void test1(void) {
	int i;

	srand ( time(NULL) );
	//sleep(10);
	for(i=0; i<1000000;i++){	
		random_numbers[i] = rand() % 10 + 1;
	}
	

}

void test2(void) {
        int i;
	gsl_rng * r = gsl_rng_alloc (gsl_rng_mt19937);
	gsl_rng_set (r, 1);
	//gsl_rng_mt19937
	//gsl_rng_taus
	
        for(i=0; i<1000000;i++){
		
                random_numbers[i] = gsl_rng_uniform_int (r, 10) +1;
        }

	gsl_rng_free (r);

}

void test_results(void) {
	int Tally[10] = { 0 };
	int Series[10] = { 0 };
	int Without[10] = { 0 };
	int i, j, k, l;
	//double percentage;
	double total=0;
	double avg_deviation = 0;
	double largest_deviation = 0;

	for(i=0; i<1000000; i++){
		for(j=1; j<11; j++){
			if(random_numbers[i] == j){
				Tally[j-1]++;
				total++;
				//if(random_numbers[i] == random_numbers[i-1] && i != 0) {
					//find out how many are the same in this series
					
					//if that number is greater than previous series entry then replace it

				//}
			}
		}
	}

	for(j=1; j<11; j++){
		l=0;
		for(i=0; i<1000000; i++){
			k = 0;		
			while(random_numbers[i] == j) {
				k++;
				i++;
			}
			if(k > l){
				l = k;
			}
		}
		Series[j-1]=l; 
		//k=0;
		//l=0;
	}



	//print results

	for(j=0; j < 10; j++) {
		printf("%d\t Total: %d %%: %lf Longest in Series: %d\n", j+1, Tally[j], (Tally[j]/total)*100, Series[j]);
	}
	printf("total rand numbers generated: %lf\n", total);

	//largest_deviation
	//abs(total/10 - (Tally[j]/total)*100);

	for(j=0; j < 10; j++) {
		if( largest_deviation < abs(total/10 - Tally[j]) ) {	
			largest_deviation = abs(total/10 - Tally[j]);
		}
		avg_deviation += abs(total/10 - Tally[j]);
	}
	avg_deviation = avg_deviation/10;

	printf("Avg Deviation: %lf\tLargest Deviation: %lf\n", avg_deviation, largest_deviation);

}



main() {


	printf("test1: generating 1 million random numbers 1-10\n");
	gettimeofday(&starttime, NULL);
	test1();
	gettimeofday(&endtime, NULL);
	time1=((double)(endtime.tv_sec*1000000-starttime.tv_sec*1000000+endtime.tv_usec-starttime.tv_usec))/1000000;
	test_results();
	printf("runtime: %lf\n\n", time1);

	printf("test2: generating 1 million random numbers 1-10\n");
	gettimeofday(&starttime, NULL);
	test2();
	gettimeofday(&endtime, NULL);
	time1=((double)(endtime.tv_sec*1000000-starttime.tv_sec*1000000+endtime.tv_usec-starttime.tv_usec))/1000000;
	test_results();
	printf("runtime: %lf\n\n", time1);



	exit(0);
}
