#include <stdlib.h>
#include <math.h>
#include <fstream.h>
#include <iostream.h>
#include <strstream.h>
#include <time.h>

int main(){
  //A simple minded program for a simple problem:  We are asked
  //in problem 1.1 to create 10,000 random numbers, and to bin them
  //in bins of length 0.01 (0,1) and to plot the histogram.  There 
  //will be 100 bins total
  ofstream outf("1.1.rand.dat");
  srand(time(NULL));
  rand();
  int NUM=10000;
  int H[99]; double R[NUM-1];  
  //General Tsao says you must first create the list of random numbers:
  for (int i = 0; i<NUM; i++){
    R[i]=1.0*rand()/(1.0*RAND_MAX);
  }//printf("%f \t",R[i]);}

  //General Tsao says you must first set all historgram to zero
  for (int i=0; i<100;i++){H[i]=0;}

  //General Tsao says you must next divide up into bins:
  //Assignment is such as 0.01 > 1, 0.02 > 2 onto 100 
  for (int j = 0; j<100;j++){
  for (int i = 0; i<NUM; i++){
    if (R[i]<=(0.01 * (j+1))) if (R[i]>=(0.01*(j))) {H[j] = H[j]+1;}}}
    
  //General Tsao says you must report your results or your results won't exist
  for (int i = 0; i<100; i++){
    outf<<H[i]<<"\n";}

  //General Tsao says perform /chi^2 test as follows:
  //in this case, n = 10000, p_S = 100 for each bin.
  double V = 0.0;
  double v = 0.0;
  for (int i = 0; i<100; i++){v = v+ (H[i]*H[i])/100;}
  V = v - 1.0*NUM;
  printf("%f ",V);

}
