Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // This program version should comply to the ADT paradigm.
- #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 or ADT.
- 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 an ordinary class with private attributes und public methods.
- class random_number_generator {
- private:
- unsigned long int max,
- factor,
- increment,
- start,
- actualrandom;
- public:
- random_number_generator():max(1000),factor(623),increment(525),start(157) {
- actualrandom = ((factor * start) + increment) % max;
- }
- unsigned int long random_number() {
- actualrandom = ((factor * actualrandom) + increment) % max;
- return actualrandom;
- }
- 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();
- }
- };
- int main(int argc, const char * argv[]) {
- random_number_generator rng1, rng2;
- if (ask_yn("Random seed the first generator") == true)
- rng1.random_seed();
- if (ask_yn("Random seed the second generator") == true)
- rng2.random_seed();
- do {
- cout << rng1.random_number() << '\t' << rng2.random_number() << endl;
- } while (ask_yn("Another pair of random numbers") == true);
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement