Advertisement
Guest User

Untitled

a guest
Nov 19th, 2019
111
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.47 KB | None | 0 0
  1. // This proram version should make use of the Abstract Data Object paradigm (ADO).
  2. // There shoud be an abstract data object module. Its only and implicitly created
  3. // instance is the abstract data object random_number_generator. It is not allowed
  4. // to create further instances of the abstract data object module.
  5. // Data of the ADO shall not be accessible from outside. There should be appropriate
  6. // functions with public access for the relevant functionality of rando_number_generator.
  7. // You should avoid any global variables.
  8.  
  9. #include <iostream> // required for console input and output
  10. #include <chrono> //required for getting system time
  11.  
  12. using namespace std; // allows unqualified use of identifiers of namespace std
  13.  
  14. // The following free function should remain as is.
  15. // It would be pointless to encapsulate it as an ADO.
  16. bool ask_yn(const char * msg) {
  17.     cout << msg << " (y/n)? " << flush;
  18.     char answer;
  19.     cin >> answer;
  20.     if (answer == 'y' || answer == 'Y')
  21.         return true;
  22.     else return false;
  23. }
  24.  
  25. // The solution is to have a class with static attributes and static methods only.
  26. class random_number_generator {
  27. private:
  28.     static unsigned long int max,
  29.     factor,
  30.     increment,
  31.     start,
  32.     actualrandom;
  33. public:
  34.     static unsigned int long random_number() {
  35.         actualrandom = ((factor * actualrandom) + increment) % max;
  36.         return actualrandom;
  37.        
  38.     }
  39.     static void random_seed() {
  40.         // the following function calls retrieve system time and cast it to milliseconds
  41.         actualrandom =  chrono::duration_cast<chrono::milliseconds>(chrono::system_clock::now().time_since_epoch()).count();
  42.     }
  43. };
  44.  
  45. // Due to C++ requirements static attributes have to be initialized outside the class.
  46. unsigned long int random_number_generator::max = 1000;
  47. unsigned long int random_number_generator::factor = 623;
  48. unsigned long int random_number_generator::increment = 525;
  49. unsigned long int random_number_generator::start = 157;
  50. unsigned long int random_number_generator::actualrandom = ((random_number_generator::factor * random_number_generator::start) + random_number_generator::increment) % random_number_generator::max;;
  51.  
  52.  
  53. int main(int argc, const char * argv[]) {
  54.     if (ask_yn("Random seed the generator") == true)
  55.         random_number_generator::random_seed();
  56.     do {
  57.         cout << random_number_generator::random_number() << endl;
  58.     } while (ask_yn("Another random number") == true);
  59.     return 0;
  60. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement