/* This file contains the definitions of the operations of class RandomInt.
    The declaration of class RandomInt can be found in RandomInt.h

   Written by:  Joel C. Adams, Spring, 1993, at Calvin College.
--------------------------------------------------------------------------*/

#include "RandomInt.h"

#include <time.h>

/*--- This is the class constructor for class RandomInt.

   Precondition:  A RandomInt object has been declared.
   Receive:       Low, the lower bound of the range of random values;
                  High, the upper bound of the range of random values.
   Postcondition: That object's members are initialized.
---------------------------------------------------------------------*/

RandomInt::RandomInt(int Low, int High)
{
   if (Low < 0)                      // check
   {
      cerr << "\n*** RandomInt: lower bound cannot "
           << "be negative (using zero).\n";
      LowerBound = 0;
   }
   else
      LowerBound = Low;

   if (High <= Low)
   {
      cerr << "\n*** RandomInt: upper bound must exceed "
           << "lower bound (using " << RAND_MAX << ").\n";
      UpperBound = RAND_MAX;
   }
   else
      UpperBound = High;

   long
      SeedVal = long(time(0));   // seed value

   srand(SeedVal);               // seed the generator

   RandomValue = LowerBound + rand() % (UpperBound - LowerBound + 1);
}

/*--- This function generates the next random number.

   Receive: Low, an optional lower bound on the range of random values;
            High, an optional upper bound on the range of random values
         (if Low and High are omitted, the constructed values are used).
   Return:  This object, its RandomValue member set to a random
              number from the range Low..High.
---------------------------------------------------------------------*/

RandomInt RandomInt::Generate(int Low, int High)
{
   if (Low || High)                   // new range specified
   {
      if (Low < 0)
      {
         cerr << "\n*** RandomInt: lower bound cannot "
              << "be negative (using zero).\n";
         LowerBound = 0;
      }
      else
         LowerBound = Low;

      if (High <= Low)
      {
         cerr << "\n*** RandomInt: upper bound must exceed "
              << "lower bound (using " << RAND_MAX << ").\n";
         UpperBound = RAND_MAX;
      }
      else
         UpperBound = High;
   }

   RandomValue = LowerBound + rand() % (UpperBound - LowerBound + 1);

   return *this;
}

/*--- This is the assignment operator for a RandomInt.

   Receive: A RandomInt object, RInt.
   Return:  This RandomInt object, its members identical to those
              of RInt.
---------------------------------------------------------------------*/

RandomInt& RandomInt::operator=(const RandomInt& RInt)
{
   LowerBound = RInt.LowerBound;
   UpperBound = RInt.UpperBound;

   RandomValue = RInt.RandomValue;

   return *this;
}

/*--- This function performs the output operation for a RandomInt.

   Recieve: An ostream and a RandomInt.
   Output:  The RandomInt onto the ostream.
   Return:  The ostream.
---------------------------------------------------------------------*/

ostream& operator<<(ostream& Out, const RandomInt& RInt)
{
   Out << RInt.RandomValue;

   return Out;
}

