RandomInt Test


RandomIntTest.cpp

Program listing

/************************************************************************
*                                                                       *
*   File:       RandomIntTest.cpp                                       *
*                                                                       *
*   Author:     Robb T. Koether                                         *
*                                                                       *
*   Date:       Nov 5, 1999                                             *
*                                                                       *
*   Abstract:   This program will test the member functions of the      *
*               RandomInt class                                         *
*                                                                       *
************************************************************************/

//  Header files

#include <iostream>
#include <string>
#include "randomint.h"

using namespace std;

/************************************************************************
*                                                                       *
*   Function:   main                                                    *
*                                                                       *
*   Purpose:    To test the member functions of the RandomInt class     *
*                                                                       *
************************************************************************/

int main() {

//  Test the constructors

    cout << "Test the constructors" << endl;
    RandomInt r;
    RandomInt s(30000);
    RandomInt t(500, 600);
    RandomInt u(1000, 2000, 12345);
    
//  Test the inspectors

    cout << endl << "Test the inspectors" << endl;
    cout << "Interval for r is [" << r.GetLow() << ", " << r.GetHigh() << "]" << endl;
    cout << "Interval for s is [" << s.GetLow() << ", " << s.GetHigh() << "]" << endl;
    cout << "Interval for t is [" << t.GetLow() << ", " << t.GetHigh() << "]" << endl;
    cout << "Interval for u is [" << u.GetLow() << ", " << u.GetHigh() << "]" << endl;

//  Test the Draw() function

    cout << endl << "Test the Draw() function" << endl;
    cout << "Random integers from r:  ";
    cout << r.Draw() << ", " << r.Draw() << ", " << r.Draw() << ", " << r.Draw() << endl;
    cout << "Random integers from s:  ";
    cout << s.Draw() << ", " << s.Draw() << ", " << s.Draw() << ", " << s.Draw() << endl;
    cout << "Random integers from t:  ";
    cout << t.Draw() << ", " << t.Draw() << ", " << t.Draw() << ", " << t.Draw() << endl;
    cout << "Random integers from u:  ";
    cout << u.Draw() << ", " << u.Draw() << ", " << u.Draw() << ", " << u.Draw() << endl;
    
//  Test the mutators

    cout << endl << "Test the mutators" << endl;
    s.SetInterval(50, 60);
    cout << "Interval for s is [" << s.GetLow() << ", " << s.GetHigh() << "]" << endl;

    r.SetSeed(12345);   
    cout << "Random integers from r:  ";
    cout << r.Draw() << ", " << r.Draw() << ", " << r.Draw() << ", " << r.Draw() << endl;

//  Test the Randomize() function

    cout << endl << endl << "Test the Randomize() function" << endl;    
    r.Randomize();
    cout << "Random integers from r:  ";
    cout << r.Draw() << ", " << r.Draw() << ", " << r.Draw() << ", " << r.Draw() << endl;
    r.SetSeed(12345);
    cout << "Random integers from r:  ";
    cout << r.Draw() << ", " << r.Draw() << ", " << r.Draw() << ", " << r.Draw() << endl;

    cout << endl << "Good-bye" << endl;
    
    return 0;
}

randomint.h

/************************************************************************
*                                                                       *
*   File:       randomint.h                                             *
*                                                                       *
*   Author:     Robb T. Koether                                         *
*                                                                       *
*   Date:       Nov 5, 1999                                             *
*                                                                       *
*   Abstract:   This file contains the definition of the RandomInt      *
*               class                                                   *
*                                                                       *
************************************************************************/

#ifndef RANDOMINT_H
#define RANDOMINT_H

//  Header files

#include <stdlib.h>

using namespace std;

/************************************************************************
*                                                                       *
*   The RandomInt class                                                 *
*                                                                       *
************************************************************************/

class RandomInt {
    
//  Public member functions

    public:
    
        RandomInt(int a = 0, int b = RAND_MAX);
        RandomInt(int a, int b, unsigned int s);

    //  Inspectors

        int GetLow() const;
        int GetHigh() const;
    
    //  Mutators
    
        void SetInterval(int a, int b);
        void SetSeed(unsigned int s) const;
    
    //  Other member functions
    
        int Draw() const;
        void Randomize() const;
    
//  Date members
    
    private:
    
        int Low;
        int High;
};

#endif

RandomInt.cpp

/************************************************************************
*                                                                       *
*   File:       RandomInt.cpp                                           *
*                                                                       *
*   Author:     Robb T. Koether                                         *
*                                                                       *
*   Date:       Nov 5, 1999                                             *
*                                                                       *
*   Abstract:   This file contains the implementations of the member    *
*               functions of the RandomInt class                        *
*                                                                       *
************************************************************************/

//  Header files

#include <iostream>
#include <string>
#include <stdlib.h>
#include <time.h>
#include "randomint.h"

using namespace std;

/************************************************************************
*                                                                       *
*   CONSTRUCTORS                                                        *
*                                                                       *
************************************************************************/

/************************************************************************
*                                                                       *
*   Function:   RandomInt()                                             *
*               RandomInt(int)                                          *
*               RandomInt(int, int)                                     *
*                                                                       *
*   Purpose:    To construct a RandomInt object with the specified      *
*               limits                                                  *
*                                                                       *
************************************************************************/

RandomInt::RandomInt(int a, int b) {
    SetInterval(a, b);
    return;
}

/************************************************************************
*                                                                       *
*   Function:   RandomInt(int, int, unsigned int)                       *
*                                                                       *
*   Purpose:    To construct a RandomInt object with the specified      *
*               limits and seed                                         *
*                                                                       *
************************************************************************/

RandomInt::RandomInt(int a, int b, unsigned int s) {
    SetInterval(a, b);
    SetSeed(s);
    return;
}

/************************************************************************
*                                                                       *
*   INSPECTORS                                                          *
*                                                                       *
************************************************************************/

/************************************************************************
*                                                                       *
*   Function:   GetLow                                                  *
*                                                                       *
*   Purpose:    To return the lower endpoint                            *
*                                                                       *
************************************************************************/

int RandomInt::GetLow() const {
    return Low;
}

/************************************************************************
*                                                                       *
*   Function:   GetHigh                                                 *
*                                                                       *
*   Purpose:    To return the upper endpoint                            *
*                                                                       *
************************************************************************/

int RandomInt::GetHigh() const {
    return High;
}

/************************************************************************
*                                                                       *
*   MUTATORS                                                            *
*                                                                       *
************************************************************************/

/************************************************************************
*                                                                       *
*   Function:   SetInterval                                             *
*                                                                       *
*   Purpose:    To set the endpoints of the interval                    *
*                                                                       *
************************************************************************/

void RandomInt::SetInterval(int a, int b) {
    if (a > b) {
        cerr << "ERROR:  Bad interval" << endl;
        cerr << "Aborting..." << endl;
        exit(1);
    }
    else {
        Low = a;
        High = b;
    }
    return;
}

/************************************************************************
*                                                                       *
*   Function:   SetSeed                                                 *
*                                                                       *
*   Purpose:    To set the seed of the random number generator          *
*                                                                       *
*   Note:       This will affect all RandomInt objects                  *
*                                                                       *
************************************************************************/

void RandomInt::SetSeed(unsigned int s) const {
    srand(s);
    return;
}

/************************************************************************
*                                                                       *
*   OTHER MEMBER FUNCTIONS                                              *
*                                                                       *
************************************************************************/

/************************************************************************
*                                                                       *
*   Function:   Draw                                                    *
*                                                                       *
*   Purpose:    To return a random integer in the interval              *
*                                                                       *
************************************************************************/

int RandomInt::Draw() const {
    int IntervalSize = GetHigh() - GetLow() + 1;
    int RandomOffset = rand() % IntervalSize;
    int Number = GetLow() + RandomOffset;
    return Number;
}

/************************************************************************
*                                                                       *
*   Function:   Randomize                                               *
*                                                                       *
*   Purpose:    To select a "random" seed for the random number         *
*               generator                                               *
*                                                                       *
*   Note:       This will affect all RandomInt objects                  *
*                                                                       *
************************************************************************/

void RandomInt::Randomize() const {
    srand((unsigned int) time(0));
    return;
}

Sample Run

Test the constructors

Test the inspectors
Interval for r is [0, 32767]
Interval for s is [30000, 32767]
Interval for t is [500, 600]
Interval for u is [1000, 2000]

Test the Draw() function
Random integers from r:  3498, 22117, 9988, 21468
Random integers from s:  31050, 30365, 32205, 30319
Random integers from t:  503, 585, 540, 527
Random integers from u:  1336, 1850, 1066, 1749

Test the mutators
Interval for s is [50, 60]
Random integers from r:  3498, 22117, 9988, 21468


Test the Randomize() function
Random integers from r:  10674, 22841, 28017, 10172
Random integers from r:  3498, 22117, 9988, 21468

Good-bye



Return to Lectures page


e-mail me at robbk@hsc.edu

This page was last modified on Fri Nov 5 16:02:30 1999 .

URL: http://people.hsc.edu/faculty-staff/robbk/Coms261/Examples/RandomIntTest.html

Return to Coms 261 home page