/******************************* RANDOM.CPP ***********************************
* Example showing the use of a class.                              1997-12-02 *
*                                                                   Agner Fog *
*                                                                             *
* The class TRandomGenerator defines a random number generator.               *
* The idea of encapsulating it into a class is that you can use it without    *
* understanding how it works, and without touching the internal variables.    *
*                                                                             *
* (Better random number generators can be found at                            *
*  www.announce.com/agner/random )                                            *
******************************************************************************/

#include <iostream.h>
#include <conio.h>
#include <math.h>
#include <time.h>

// Define random number generator class
class TRandomGenerator {
   public:
   TRandomGenerator(void);  // constructor
   void Initialize(double seed); // initialize random sequence
   double FRandom(void);  // random floating point number between 0 and 1
   int IRandom(void); // random integer number within defined interval
   void SetInterval(int min, int max); // set interval for IRandom
   private:
   double ran;  // last random number
   int rmin, rmax; // interval for IRandom
   };

// Constructor
TRandomGenerator::TRandomGenerator() {
   Initialize(time(0));} // initialize to random time

// Initialize sequence.
// Call this function with any number for seed to start a new sequence of
// random numbers.
void TRandomGenerator::Initialize(double seed) {
   rmin=0; rmax=1;
   ran = seed * M_PI;        // get some funny decimals
   FRandom(); FRandom();}    // advance twice

// Get a random floating point number between 0 and 1
double TRandomGenerator::FRandom(void) {
   ran = fmod(ran*9821.+0.211327, 1.);
   return ran;}

// Get a random integer number in interval set by SetInterval
int TRandomGenerator::IRandom(void) {
   int ir = rmin + FRandom()*(rmax-rmin+1);
   if (ir > rmax) ir = rmax;
   return ir;}

// set interval for IRandom
void TRandomGenerator::SetInterval(int min, int max) {
   rmin = min;  rmax = max;}


// This is an example showing how to output 20 random numbers
void main() {
   int i;  TRandomGenerator rg;
   rg.SetInterval(0, 99);
   for (i=0; i<20; i++) cout << "\n" << rg.IRandom();
   getch();}



/************************************************************************
 Exercise: Use TRandomGenerator to make a soccer pools (tips) coupon.

 PS. If you win then you owe me 10% commission for letting you use my
 superior random number generator. If you loose, then it's your own fault:
 why are you wasting money on something with such a low rate of payback?
************************************************************************/
