Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // This proram version should make use of the Abstract Data Object paradigm (ADO).
- // There shoud be an abstract data object module. Its only and implicitly created
- // instance is the abstract data object random_number_generator. It is not allowed
- // to create further instances of the abstract data object module.
- // Data of the ADO shall not be accessible from outside. There should be appropriate
- // functions with public access for the relevant functionality of rando_number_generator.
- // You should avoid any global variables.
- #include <iostream> // required for console input and output
- #include <chrono> //required for getting system time
- using namespace std; // allows unqualified use of identifiers of namespace std
- // The following free function should remain as is.
- // It would be pointless to encapsulate it as an ADO.
- bool ask_yn(const char * msg) {
- cout << msg << " (y/n)? " << flush;
- char answer;
- cin >> answer;
- if (answer == 'y' || answer == 'Y')
- return true;
- else return false;
- }
- // The solution is to have a class with static attributes and static methods only.
- class random_number_generator {
- private:
- static unsigned long int max,
- factor,
- increment,
- start,
- actualrandom;
- public:
- static unsigned int long random_number() {
- actualrandom = ((factor * actualrandom) + increment) % max;
- return actualrandom;
- }
- static void random_seed() {
- // the following function calls retrieve system time and cast it to milliseconds
- actualrandom = chrono::duration_cast<chrono::milliseconds>(chrono::system_clock::now().time_since_epoch()).count();
- }
- };
- // Due to C++ requirements static attributes have to be initialized outside the class.
- unsigned long int random_number_generator::max = 1000;
- unsigned long int random_number_generator::factor = 623;
- unsigned long int random_number_generator::increment = 525;
- unsigned long int random_number_generator::start = 157;
- unsigned long int random_number_generator::actualrandom = ((random_number_generator::factor * random_number_generator::start) + random_number_generator::increment) % random_number_generator::max;;
- int main(int argc, const char * argv[]) {
- if (ask_yn("Random seed the generator") == true)
- random_number_generator::random_seed();
- do {
- cout << random_number_generator::random_number() << endl;
- } while (ask_yn("Another random number") == true);
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement