Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <ostream>
- #include <string>
- #include <time.h>
- using namespace std; // allows unqualified use of identifiers of namespace std
- class Singleton
- {
- private:
- /* Here will be the instance stored. */
- static Singleton* instance;
- /* Private constructor to prevent instancing. */
- Singleton();
- int max;
- int factor;
- int increment;
- unsigned long int actualrandom;
- public:
- /* Static access method. */
- static Singleton* getInstance();
- bool ask_yn(const char * msg);
- void random_number();
- void random_seed();
- };
- /* Null, because instance will be initialized on demand. */
- Singleton* Singleton::instance = 0;
- Singleton* Singleton::getInstance()
- {
- if (instance == 0)
- {
- instance = new Singleton();
- }
- return instance;
- }
- Singleton::Singleton(){
- this->max=1000;
- this->factor=623;
- this->increment=525;
- this->actualrandom=((this->factor*157)+this->increment)%this->max;
- }
- bool Singleton::ask_yn(const char * msg) {
- cout << msg << " (y/n)? " << flush;
- char answer;
- cin >> answer;
- if (answer == 'y' || answer == 'Y')
- return true;
- else return false;
- }
- void Singleton::random_number() {
- this->actualrandom = ((this->factor * this->actualrandom) + this->increment) % this->max;
- cout<<this->actualrandom<<"\n";
- }
- void Singleton::random_seed(){
- time_t timer;
- time(&timer);
- // timer*100 gives negative number
- this->actualrandom= timer%this->max;
- }
- int main(){
- //new Singleton(); // Won't work
- Singleton* s = Singleton::getInstance(); // Ok
- if(s->ask_yn("Random seed the generator")==true){
- s->random_seed();
- }
- do{
- s->random_number();
- }while(s->ask_yn("Another random number?"));
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement