#include // for time() #include // for rand/srand #include "rando.h" int RandGen::ourInitialized = 0; RandGen::RandGen() // postcondition: system srand() used to initialize seed // once per program { if (0 == ourInitialized) { ourInitialized = 1; // only call srand once srand(time(0)); // randomize } } RandGen::RandGen(int seed) // postcondition: system srand() used to initialize seed // once per program { if (0 == ourInitialized) { ourInitialized = 1; // only call srand once srand(seed); // randomize } } void RandGen::SetSeed(int seed) // postcondition: system srand() used to initialize seed // once per program (this is a static function) { if (0 == ourInitialized) { ourInitialized = 1; // only call srand once srand(seed); // randomize } } int RandGen::RandInt(int max) // precondition: max > 0 // postcondition: returns int in [0..max) { return static_cast(RandReal() * max); } int RandGen::RandInt(int low, int max) // precondition: low <= max // postcondition: returns int in [low..max] { return low + RandInt(max-low+1); } double RandGen::RandReal() // postcondition: returns double in [0..1) { return rand() / (static_cast(RAND_MAX) + 1); }