// dice.h // Metrowerks C++ version /* class for simulating a die to generate an integer random number. dice(int sides) -- constructor, sides specifies number of "sides" for the die, 2 for a coin, 6 for a regular die, etc. int roll() -- returns the random roll of the die, a (hopefully) uniformly distributed random number between 1 and # of sides int numSides() -- access function, returns # of sides int numRolls() -- access function, returns # of times roll has been called for an instance of the class Modeled after dice class from Owen Astrachan, Duke University. A Computer Science Tapestry, McGraw Hill, Custom College Series, © 1996. Last revised, Cary Matsuoka, 5/30/96 */ #ifndef _DICE_H #define _DICE_H #include // srand() and rand() are inside of stdlib.h #include #include class dice{ public: dice(int sides); // constructor int roll(); // return the random roll int numSides(); // returns how many sides on die long numRolls(); // returns # times this die has been rolled private: int mySides; long myRollCount; }; dice::dice(int sides) // postcondition: all private fields initialized // randomize() called to set random number generator { long time = clock(); int seed = time % INT_MAX; srand(seed); myRollCount = 0; mySides = sides; } int dice::roll() // postcondition: # of rolls updated, returns random die roll { int temp; temp = rand(); // rand() returns a integer ranging from 0-INT_MAX temp %= mySides; myRollCount++; cout << "roll in old dice.h" << endl; // just checking return temp+1; } int dice::numSides() // postcondition: return # of sides of die { return mySides; } long dice::numRolls() // postcondition: return # times die has been rolled { return myRollCount; } #endif