Advertisement
Guest User

Untitled

a guest
Nov 19th, 2019
79
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.83 KB | None | 0 0
  1. // This program version should comply to the ADT paradigm.
  2.  
  3. #include <iostream> // required for console input and output
  4. #include <chrono> //required for getting system time
  5.  
  6. using namespace std; // allows unqualified use of identifiers of namespace std
  7.  
  8. // The following free function should remain as is.
  9. // It would be pointless to encapsulate it as an ADO or ADT.
  10. bool ask_yn(const char * msg) {
  11.     cout << msg << " (y/n)? " << flush;
  12.     char answer;
  13.     cin >> answer;
  14.     if (answer == 'y' || answer == 'Y')
  15.         return true;
  16.     else return false;
  17. }
  18.  
  19. // The solution is to have an ordinary class with private attributes und public methods.
  20. class random_number_generator {
  21. private:
  22.     unsigned long int max,
  23.                       factor,
  24.                       increment,
  25.                       start,
  26.                       actualrandom;
  27. public:
  28.     random_number_generator():max(1000),factor(623),increment(525),start(157) {
  29.         actualrandom = ((factor * start) + increment) % max;
  30.     }
  31.     unsigned int long random_number() {
  32.         actualrandom = ((factor * actualrandom) + increment) % max;
  33.         return actualrandom;
  34.        
  35.     }
  36.     void random_seed() {
  37.         // the following function calls retrieve system time and cast it to milliseconds
  38.         actualrandom =  chrono::duration_cast<chrono::milliseconds>(chrono::system_clock::now().time_since_epoch()).count();
  39.     }
  40. };
  41.  
  42. int main(int argc, const char * argv[]) {
  43.     random_number_generator rng1, rng2;
  44.     if (ask_yn("Random seed the first generator") == true)
  45.         rng1.random_seed();
  46.     if (ask_yn("Random seed the second generator") == true)
  47.         rng2.random_seed();
  48.    do {
  49.         cout << rng1.random_number() << '\t' << rng2.random_number() << endl;
  50.     } while (ask_yn("Another pair of random numbers") == true);
  51.     return 0;
  52. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement