/***
 * test3
 *
 * Plot slices of some random numbers triples.
 *
 ***/

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

#include "jims1Rand.h"

#define SYSRAND  0
#define JIM1RAND 1

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

// This generates triples (x,y,z) of randoms, and prints
// x and y if z is in the given slize.
// Inputs are 
//   seed	  an unsigned int to initialize the generator
//   N		  number of randoms to look at
//   minZ, maxZ   slice bounds
//   RandMax	  randoms are 0..(RandMax-1)
//   theSrand()   function which initialized the generator
//   theRand()	  function to return the next random
//
void printRandomSlice( int seed, int N,
		       double minZ, double maxZ, int RandMax, 
		       void theSrand(unsigned int), int theRand() ){
  int i, r;
  double ITER_MAX = 1.0e6;
  double iter;
  double x,y,z;
  theSrand(seed);
  i=0;
  iter=0.0;
  while (i<N && iter<ITER_MAX){
    iter++;
    x = ((double)theRand())/RandMax;
    y = ((double)theRand())/RandMax;
    z = ((double)theRand())/RandMax;
    if ( (z>minZ) && (z<maxZ)){
      printf("   %8.6f  %8.6f \n", x, y);
      i++;
    }
  }
  if (iter>=ITER_MAX){
    printf("  -- ITERATION LIMIT = %f hit ------\n", ITER_MAX);
  }

}

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

void printHelpAndExit(char* programName){
  printf("Usage: %s randRoutine nPoints zMin zMax  \n", programName);
  printf("  with   randRoutine : (int) 0=>rand(), 1=>jims1Rand() \n");
  printf("         nPoints     : (int) the number of randoms to output.\n");
  printf("         zMin, zMax  : (double) >0.0, <1.0 boundries of slice.\n");
  exit(0);
}

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

int string2integer(char* theString){
  int i, answer, powerOfTen, decimalDigits, scale;
  scale = 1;
  answer = 0;
  powerOfTen = 1;
  decimalDigits = 0;
  for (i=strlen(theString)-1; i>=0; i--){
    answer += powerOfTen*((int)theString[i] - (int)'0');
    powerOfTen *= 10;
  }
  return answer;
}

double string2double(char* theString){
  int i, decimalDigits;
  double answer, powerOfTen;
  decimalDigits = 0;
  answer = 0.0;
  powerOfTen = 1.0;
  for (i=strlen(theString)-1; i>=0; i--){
    //printf(" i = %d, theString[i] = %c \n", i, theString[i]);
    if ( theString[i] == '.' ){
      decimalDigits = strlen(theString)-1-i;
      //printf(" Just set decimalDigits = %d\n", decimalDigits);
      continue;
    }
    answer += powerOfTen*((int)theString[i] - (int)'0');
    //printf(" answer just increased to %f\n", answer);
    powerOfTen *= 10.0;
  }
  while (decimalDigits > 0){
    answer /= 10.0;
    //printf(" answer just decreased to %f\n", answer);
    decimalDigits--;
  }
  return answer;
}

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


int main(int argc, char* argv[]){
  int nPoints, whichFunc;
  int seed = 1234;
  double zMin, zMax;

  // If we've been passed too few arguments on command line, give some hints.
  if (argc<5){
    printHelpAndExit(argv[0]);
  }
  whichFunc = string2integer(argv[1]);
  nPoints   = string2integer(argv[2]);
  zMin	    = string2double(argv[3]);	
  zMax	    = string2double(argv[4]);	

  // printf(" whichFunc = %d \n", whichFunc);
  // printf(" nPoints   = %d \n", nPoints);
  // printf(" zMin      = %f \n", zMin);
  // printf(" zMax      = %f \n", zMax);
  // exit(0);

  if ( zMin<0.0 || zMin>1.0 || zMax<0.0 || zMax>1.0 || zMax<=zMin ) {
    printf("Oops: zMin, zMax must be >0, <1, and zMin<zMax \n");
    printHelpAndExit(argv[0]);
  }

  switch (whichFunc) {
  case SYSRAND:      
    printRandomSlice(seed, nPoints, zMin, zMax, 
		     RAND_MAX, srand, rand );
    break;
  case JIM1RAND:
    printRandomSlice(seed, nPoints, zMin, zMax, 
		     jims1GetRAND_MAX(), jims1Srand, jims1Rand );
    break;
  default:
    printf("Oops: illegal randRoutine value '%c'.\n", *argv[1]);
    printHelpAndExit(argv[0]);
  }


}


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

  // -- debug command line argument passing
  // printf(" argc is '%d'. \n", argc);
  // for (i=0; i<argc; i++){
  //   printf(" argv[%d] = '%s'.\n", i, argv[i]);
  // }


syntax highlighted by Code2HTML, v. 0.9.1