Headers/Random.h

/*
    File:       Random.h
 
    Contains:   TRandom is a stackbased utility class for random number generation.
                TRandom.h contains the header file information for the class.
 
    Written by:     
 
    Copyright:  Copyright © 1991-1999 by Apple Computer, Inc., All Rights Reserved.
 
                You may incorporate this Apple sample source code into your program(s) without
                restriction. This Apple sample source code has been provided "AS IS" and the
                responsibility for its operation is yours. You are not permitted to redistribute
                this Apple sample source code as "Apple sample source code" after having made
                changes. If you're going to re-distribute the source, we require that you make
                it clear in the source that the code was descended from Apple sample source
                code, but that you've made changes.
 
    Change History (most recent first):
                8/18/1999   Karl Groethe    Updated for Metrowerks Codewarror Pro 2.1
                
 
*/
// Declare label for this header file
#ifndef _RANDOM_
#define _RANDOM_
 
 
// HEADER FILES
#ifndef _DTSCPLUSLIBRARY_
#include "DTSCPlusLibrary.h"
#endif
 
//  TOOLBOX INCLUDES
#ifndef __TYPES__
#include <Types.h>
#endif
 
#ifndef __OSUTILS__
#include <OSUtils.h>
#endif
 
#ifndef __kQUICKDRAW__
#include <Quickdraw.h>
#endif
 
#ifndef __EVENTS__
#include <Events.h>
#endif
 
//  EXTERNAL FUNCTIONS
 
 
// _________________________________________________________________________________________________________ //
//  TRandom Interface
class TRandom
// The Random number class will produce random numbers within a certain range
// which could be specified. Compared with the Toolbox Random function 
// TRandom is also able to swith between various alternate random algorithms
// and the random generator could be cloned using copy constructors
{
public:
    //  TYPEDEFS AND ENUMS
    enum ERandType
    {
        kMACOS, kSHUFFLE, kPM
    };                                          // selected random number generator
 
    //  CONSTRUCTORS & DESTRUCTORS
    TRandom(ERandType = kMACOS,
            long theSeed = 1,
            unsigned short low = 0,
            unsigned short high = 100);         // default constructor
 
    TRandom(const TRandom&);                    // copy constructor
    TRandom& operator=(const TRandom&);         // assignment operator
    virtual~ TRandom();                         // virtual destructor
 
protected:
    Boolean IRandom(ERandType);                 // initialize needed class information
 
public:
    //  MAIN INTERFACES
    unsigned short Next();                      // get next random number
    TRandom& ShuffleSeed();                     // set seed value
    TRandom& SetRandomGenerator(ERandType);     // select random number algorithm
 
    //  PUBLIC ACCESSORS AND MUTATORS
    long GetSeedValue() const;                  // get current seed value
    TRandom& SetSeedValue(const long theSeed);  // get seed value and return a this pointer to a class
    ERandType GetAlgorithmType() const;         // get currently used algoritm type
 
protected:
    //  RANDOM GENERATOR ALGORITHMS/MEMBER FUNCTIONS
    unsigned short MacRandom();                 // Toolbox Random function
    unsigned short ShuffleRandom();             // shuffle Toolbox random values
    TRandom& InitShuffleRandom();               // initialize the shuffle algorithm
    unsigned short ParkMiller();                // Park&Miller Random generator
 
 
protected:
    //  TYPEDEFS AND ENUMS (INTERNAL IMPLEMENTATION)
    enum eSIZE
    {
        kSHUFFLETABLE = 128, kPM1 = 2836, kPM2 = 16807, k16BIT = 65536, kPM3 = 127773, kMSEED = 1618003398, eBIG = 1000000000, kACM_MAX = 2147483647
    };
 
 
    typedef unsigned short(TRandom::* RandGen)();// ptr to actual selected random routine
 
    //  FIELDS
    RandGen fGenerator;                         // selected random generator function
    ERandType fAlgorithm;                       // selected algorithm
    unsigned short fLow;                        // lower limit of random number
    unsigned short fHigh;                       // higher limit of random number
    unsigned short fRange;                      // max size random number
    long fSeed;                                 // random generator seed
    Boolean fState;                             // current state of object, OK or BAD
    unsigned short fPrevNum;                    // previous random number
    unsigned short fShuffleBuf[kSHUFFLETABLE];  // buffer for the shuffle algorithm
};
 
 
#endif 
 
// _________________________________________________________________________________________________________ //
 
 
/*  Change History (most recent last):
  No        Init.   Date        Comment
  1         khs     6/2/92      New file
  2         khs     1/7/93      Cleanup
*/