//
//
//  analyze.cc
//
//      AUTHOR: Russell Lowke
//         T.F: Francis Crick
//  CSCIE-s111: Unit Four, Easy Supp - question 4 [worth 2pts],  7/10/2002
//

#include <iostream>
#include <iomanip>
#include <cstdlib>
#include <ctime>


// constants
const int  OUTS_C     = 634; //  chance of an out
const int  WALKS_C    = 103; //  chance of a walk
const int  SINGLES_C  = 190; //  chance of a single
const int  DOUBLES_C  = 49;  //  chance of a double
const int  TRIPLES_C  = 11;  //  chance of a triple
const int  HOME_C     = 13;  //  chance of a home     


//
// random seed according to time
//
void randomizer()
{
	time_t      timeval;
	
	time(&timeval);
	srand(timeval);
}


//
// Random generator
//
int randomNum(int min, int max)
{
	static bool seed = false;
	int range = (max+1) - min;
	    
	if (! seed) {
		randomizer();
		seed = true;
	}
	
    return (int) (( rand() / (double) RAND_MAX ) * range + min);
}



//
// playball n times
//
void playBall(int n)
{
     int    rndNum     = 0;             //  random batting value
     int    outs       = 0;             //  number of outs
     int    singles    = 0;             //  singles scored
     int    triples    = 0;             //  triples scored
     int    walks      = 0;             //  walks taken
     int    doubles    = 0;             //  number of doubles
     int    home       = 0;             //  number of home runs
     int    maxValue   = OUTS_C    +
                         SINGLES_C +
                         TRIPLES_C +
                         WALKS_C   +
                         DOUBLES_C +
                         HOME_C;        //  sould be 1000
     double battingAve = 0;             //  batting average

     for (int count = 1; count <= n; count++) {
        rndNum = randomNum(1, maxValue);
        
        if (rndNum      >= OUTS_C    +
                           SINGLES_C +
                           TRIPLES_C +
                           WALKS_C   +
                           DOUBLES_C)
            home++;                     //  Home run
        else if (rndNum >= OUTS_C    +
                           SINGLES_C +
                           TRIPLES_C +
                           WALKS_C)
            doubles++;                  //  Double
        else if (rndNum >= OUTS_C    +
                           SINGLES_C +
                           TRIPLES_C)
            walks++;                    //  Walk
        else if (rndNum >= OUTS_C    +
                           SINGLES_C)
            triples++;                  //  Triple
        else if (rndNum >= OUTS_C)
            singles++;                  //  Single
        else
            outs++;                     //  Out
        
    }
    
    cout << endl;
    cout << n << " times at bat for Johann\n";
    cout << endl;
    cout << "Home: "    << home    << endl;
    cout << "Doubles: " << doubles << endl;
    cout << "Walks: "   << walks   << endl;
    cout << "Triples: " << triples << endl;
    cout << "Singles: " << singles << endl;
    cout << "Outs: "    << outs    << endl;
    cout << endl;
    
    battingAve = (singles + doubles + triples + home)
                 / (double) (1000 - walks);
    cout << "Batting Average: ";
    cout << battingAve;
    cout << endl << endl;
}


void main()
{
    // play ball - 1000 bats
    playBall(1000);
	
	exit( 0 );
}