/***
 * test2
 *
 * Plot pairs or triples of random numbers.
 *
 ***/

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

#include "jims1Rand.h"

#define SYSRAND  0
#define JIM1RAND 1

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

// Prints out pairs of randoms.
// Inputs are 
//   seed	  an unsigned int to initialize the generator
//   N		  number of randoms to look at
//   RandMax	  randoms are 0..(RandMax-1)
//   theSrand()   function which initialized the generator
//   theRand()	  function to return the next random
//
void printRandoms( int seed, int N, int dimensions, int RandMax, 
		      void theSrand(unsigned int), int theRand() ){
  int i, d;
  theSrand(seed);
  for (i=0; i<N; i++){
    for (d=0; d<dimensions; d++){
      printf("  %8.6f", ((double)theRand())/RandMax);
    }
    printf("\n");
  }
}

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

void printHelpAndExit(char* programName){
  printf("Usage: %s randRoutine nPoints nDim\n", programName);
  printf("  where randRoutine is an integer, 0=>rand(), 1=>jims1Rand(), \n");
  printf("        nDim is an integer (2 or 3) = points per line, \n");
  printf("  and   nPoints is the number of lines to output.\n");
  exit(0);
}

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

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

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


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

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

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

  if (nDim<1 || nDim>10){
    printf("Oops: illegal nDim value, should be >1 and <10.\n");
    printHelpAndExit(argv[0]);
  }

  switch (whichFunc) {
  case SYSRAND:      
    printRandoms(seed, nPoints, nDim, 
		 RAND_MAX, srand, rand );
    break;
  case JIM1RAND:
    printRandoms(seed, nPoints, nDim, 
		 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