/***
 * test1
 *
 * Look at the statistics of some random number 
 * generators.
 *
 ***/

#include <stdlib.h>
#include <stdio.h>
#include <math.h>

#include "jims1Rand.h"

// -----------------------------------------------------

// This prints out some randoms as well as their mean and
// standard deviation (sigma).
// Inputs are 
//   seed	  an unsigned int to initialize the generator
//   N		  number of randoms to look at
//   RandMax	  randoms are 0..(RandMax-1)
//   verbose	  1 => print each random
//   theSrand()   function which initialized the generator
//   theRand()	  function to return the next random
//
void testRandoms( int seed, int N, int RandMax, int verbose, 
		      void theSrand(unsigned int), int theRand() ){
  int i, r;
  double sum, sumsq, mean, sigma, x, trueSigmaN;
  printf(" Testing randoms with seed=%d, N=%d, max=%d \n", 
	 seed, N, RandMax);
  theSrand(seed);
  sum=0;
  sumsq=0;
  for (i=0; i<N; i++){
    r = theRand();
    x = ((double)r)/RandMax;
    if (verbose==1){
      printf("   %12d   %8.6f \n", r, x);
    }
    sum += x;
    sumsq += x*x;
  }
  mean = sum/N;
  sigma = sqrt( sumsq/N - mean*mean );

  printf(" When scaled to 0.0 .. 1.0 we have \n");
  printf(" Scaled to 0->1, mean = %-8.6f, sigma = %8.6f \n", mean, sigma);
  trueSigmaN = sqrt( (1.0/3.0 - 1.0/4.0) );
  printf(" (Uniform is mean=0.5, sigma_N = %8.6f \n", trueSigmaN);
  printf("\n");
    
}

// ------------------------------------------------------------

int main(){

  printf("** system srand() and rand() **\n");
  testRandoms( 11234, 100, RAND_MAX, 1, srand, rand );

  printf("** 'jims1Srand() and jims1Rand() **\n");
  testRandoms( 123, 100, jims1GetRAND_MAX(), 1, jims1Srand, jims1Rand );

}


syntax highlighted by Code2HTML, v. 0.9.1