/* Central Limit Theorem Demonstration: For lab
      tried to remove all the GSL calls and replace them with standard mathIO (inferior) rand(). 
 */ 
#include <stdio.h>
#include <stdlib.h>
//#include <gsl/gsl_rng.h>
/* generates flat random numbers between 0.0 and 1.0 */
/*    BUT, put a hole in that it gives no randoms between .35 and .45
         and has a dip from .73 to .83
      And has option of averaging a number of the random numbers before printing. 	 
*/ 
int main (int argc, char *argv[])
{
  //  const gsl_rng_type * T;
  //  gsl_rng * r;
  double u, uprev, avgout; 
  int i, n, j, flag, avgn, nout, seed;
  //gsl_rng_env_setup();

  //T = gsl_rng_default;
  //r = gsl_rng_alloc (T);
  nout = atoi(argv[1]);    // total number of numbers you want
  avgn = atoi(argv[2]);  // how many to average at a time
  seed = atoi(argv[3]);  // random seed....how many numbers to skip before starting. 
  printf("RAND_MAX: %i\n",  RAND_MAX);
  // go through seeds 
  for (i = 0; i <seed+1; i++) { 
    //double u = gsl_rng_uniform (r);
    double u = (rand()/1.0); 
  } 
  // uber-loop for averaging a certain number of random numbers, doing it nout times 
  for (j = 0; j<nout; j++){ 
  // some initializations before the underlying distribution 
  uprev = 0.0; 
  flag = 1; 
  n = avgn; 
  avgout = 0; 
  // central loop to generate the original distribution
  for (i = 0; i <n; i++) 
    {
      flag =1;  // printing flag
      //double u = gsl_rng_uniform (r);
      double u = ((double) rand())/((double) RAND_MAX); 
      // here put in the 'dings' to the distribution, just to make it interesting! 
      if ( (u<.35)&&(u>.25)){
        n=n+1;
	flag = 0; //don't pass any in this range. 
      } 
      if ( ((u<.83)&&(u>.73))&&(uprev <.25)) {
	n=n+1; 
	flag =0; // don't pass
      } 
      uprev = u; 
      //      u = u-.5; // center for simplicity, before averaging/displaying
      avgout = avgout + u*(double)(flag); 
      // print them out...for testing purposes. 
      // printf("%.5f\n", u);
    }
  avgout = avgout/(double)(avgn); 
     printf("%.7f\n", avgout);
  }
  //  gsl_rng_free (r);

  return 0;
}
