Sources/RandomTest.cp

/*
    File:       RandomTest.cp
 
    Contains:   
 
    Written by: TRandom is a stackbased utility class for random number generation.
                TRandomTest.cp contains the test functions for the TRandom class.
 
    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
                
 
*/
// HEADERS
#ifndef _RANDOM_
#include "Random.h"
#endif
 
// GLOBALS
void CalcTime(const char* string);              // calculate time function
unsigned long timeStart;                        // global timing
 
 
// Test the TRandom class inside out.
 
void main(void)
{
    const short cARRAYSIZE = 80;                // 200 is a nice size
    const long cBIGNUM = 1000;                  //cBIGNUM= 1000000 takes about 40 sec
    unsigned short val;
    long x;
    unsigned short array1[cARRAYSIZE],
     array2[cARRAYSIZE];
 
    InitGraf((Ptr) & qd.thePort);               // Initialize grafport!
 
    //  First initial quick test of the toolbox value
    qd.randSeed = TickCount();
    long xx = Random();
    cerr << "Normal Toolbox Random  = " << xx << '\n';
 
 
    //  1. Create an instance of each TRandom class
    TRandom aRandom;
    val = aRandom.Next();
    cerr << "Toolbox aRandom.Next   = " << val << "\n";
 
    aRandom.SetRandomGenerator(TRandom::kSHUFFLE);
    val = aRandom.Next();
    cerr << "Shuffle aRandom.Next   = " << val << '\n';
 
    aRandom.SetRandomGenerator(TRandom::kPM);
    val = aRandom.Next();
    cerr << "PM aRandom.Next            = " << val << '\n';
 
    //  2. Efficiency test, docBIGNUMtimes random numbers and return the time
    cerr << "Doing " << cBIGNUM << " random numbers of each class...this will take some time\n";
 
    // Toolbox Random   
    aRandom.SetRandomGenerator(TRandom::kMACOS);
    GetDateTime(&timeStart);
    for (x = 0; x <= cBIGNUM; x++)
    {
        val = aRandom.Next();
#if DEBUG
        cerr << "Toolbox Random " << x << " = " << val << '\n';
#endif
 
    }
    CalcTime("Toolbox");
 
    // Shuffle Random   
    aRandom.SetRandomGenerator(TRandom::kSHUFFLE);
    GetDateTime(&timeStart);
    for (x = 0; x <= cBIGNUM; x++)
    {
        val = aRandom.Next();
#if DEBUG
        cerr << "Shuffle Random " << x << " = " << val << '\n';
#endif
 
    }
    CalcTime("Shuffle");
 
    // PM Random
    aRandom.SetRandomGenerator(TRandom::kPM);
    GetDateTime(&timeStart);
    for (x = 0; x <= cBIGNUM; x++)
    {
        val = aRandom.Next();
#if DEBUG
        cerr << "PM Random " << x << " = " << val << '\n';
#endif
 
    }
    CalcTime("PM");
 
    //  3. Performance test, create twice the random numbers with the same
    //  original seed, and compare the results, signal if they differ
 
    long definedSeed;
 
    cerr << "Performance test, create from same seed twice and compare...\n";
 
    //  MACOS Random Performance test
    cerr << "MacOS Random...";
    aRandom.SetRandomGenerator(TRandom::kMACOS);
 
    aRandom.ShuffleSeed();                      // get new seed
    definedSeed = aRandom.GetSeedValue();       // store it
 
    aRandom.SetSeedValue(definedSeed);          // back to first seed
 
    for (x = 0; x < cARRAYSIZE; x++)            // fill array1
        array1[x] = aRandom.Next();
 
    aRandom.SetSeedValue(definedSeed);          // back to first seed
    for (x = 0; x < cARRAYSIZE; x++)            // fill array2
        array2[x] = aRandom.Next();
 
    for (x = 0; x < cARRAYSIZE; x++)
    {                                           // compare
        if (array1[x] != array2[x])
        {
            cerr << "problems with Toolbox values....\n";
            cerr << "x = " << x << ",array1 = " << array1[x] << ", array2 = " << array2[x] << '\n';
        }
        else
            cerr << ".";
    }
    cerr << "\n";
 
    //  Shuffle Random Performance test
    cerr << "Shuffle Random...";
    TRandom rand1(TRandom::kSHUFFLE);
    TRandom rand2(TRandom::kSHUFFLE);
 
    aRandom.ShuffleSeed();                      // get new seed
    definedSeed = aRandom.GetSeedValue();       // store it
 
    rand1.SetSeedValue(definedSeed);            // back to first seed
 
    for (x = 0; x < cARRAYSIZE; x++)            // fill array1
        array1[x] = rand1.Next();
 
    rand2.SetSeedValue(definedSeed);            // back to first seed
    for (x = 0; x < cARRAYSIZE; x++)            // fill array2
        array2[x] = rand2.Next();
 
    for (x = 0; x < cARRAYSIZE; x++)
    {                                           // compare
        if (array1[x] != array2[x])
        {
            cerr << "problems with Quick values....\n";
            cerr << "x = " << x << ",array1 = " << array1[x] << ", array2 = " << array2[x] << '\n';
        }
        else
            cerr << ".";
    }
    cerr << "\n";
 
    //  Park-Miller
    cerr << "PM Random...";
    aRandom.SetRandomGenerator(TRandom::kPM);
 
    aRandom.ShuffleSeed();                      // get new seed
    definedSeed = aRandom.GetSeedValue();       // store it
 
    aRandom.SetSeedValue(definedSeed);          // back to first seed
 
    for (x = 0; x < cARRAYSIZE; x++)            // fill array1
        array1[x] = aRandom.Next();
 
    aRandom.SetSeedValue(definedSeed);          // back to first seed
 
    for (x = 0; x < cARRAYSIZE; x++)            // fill array2
        array2[x] = aRandom.Next();
 
    for (x = 0; x < cARRAYSIZE; x++)
    {                                           // compare
        if (array1[x] != array2[x])
        {
            cerr << "problems with PM values....\n";
            cerr << "x = " << x << ",array1 = " << array1[x] << ", array2 = " << array2[x] << '\n';
        }
        else
            cerr << ".";
    }
    cerr << "\n";
 
    //  4. Test mutator class reference passing (syntax check)
    aRandom.SetRandomGenerator(TRandom::kMACOS).ShuffleSeed();
 
    //  5. Test of copy constructor
    cerr << "\nTest of copy constructor\n";
 
    TRandom bRandom(aRandom);
    cerr << "Seed from aRandom = " << aRandom.GetSeedValue() << "\n";
    cerr << "Seed from bRandom = " << bRandom.GetSeedValue() << "\n";
    cerr << "Rand val from aRandom = " << aRandom.Next() << "\n";
    cerr << "Rand val from bRandom = " << bRandom.Next() << "\n";
 
 
    //  6. Test of assignment operator overload
    cerr << "\nTest of assignment operator overload\n";
 
    TRandom cRandom = aRandom;
    cerr << "Seed from aRandom = " << aRandom.GetSeedValue() << "\n";
    cerr << "Seed from cRandom = " << cRandom.GetSeedValue() << "\n";
    cerr << "Rand val from aRandom = " << aRandom.Next() << "\n";
    cerr << "Rand val from cRandom = " << cRandom.Next() << "\n";
 
    cerr << "The end of the TRandom Test!\n";
}
 
 
void CalcTime(const char* string)
// Calculate Time.
{
    unsigned long totalTime, timeEnd;
 
    ::GetDateTime(&timeEnd);
    totalTime = timeEnd - timeStart;
    cerr << "Time for \t" << string << "\t= " << totalTime << '\n';
}
 
 
// _________________________________________________________________________________________________________ //
 
 
/*  Change History (most recent last):
  No        Init.   Date        Comment
  1         khs     6/2/92      New file
  2         khs     1/7/93      Cleanup
*/