/**************************************************************** Copyright (C) : University of Michigan Project: NERS 544 - Class example File: mcrng32.cpp Purpose: An example of a multiplicative congruential random number generator in 32 bits Compiler: g++ -ansi -pedantic -Wconversion Use -O2 as well Programmer: Alex Bielajew Revision Date: 09/12/2016 ****************************************************************/ #include using namespace std; int rand(int in) {return 69069*in;} // The magic multiplier 69069 is cited by Marsaglia as the "best" for 32 bits int main(void) { cout << "Starting seed = "; int iFirst; cin >> iFirst; if (iFirst%2 == 0) { cout << "*** Starting seed must be an odd number. Exiting! ***\n"; return 1; } int in = iFirst; in = rand(in); //Next in series unsigned int i = 0; cout << iFirst << endl; while(in != iFirst) { if (i < 10) cout << in << endl; i = i + 1; in = rand(in); // Update int N = 10000000; if (i%N == 0) cout << "." << flush; } while (in != iFirst); // Stops when repeats if (i < 2000000000) cout << "\n# loops = " << i + 1 << "\n\n"; else cout << "\n# loops = " << i << " + 1 = " << i + 1 << "\n\n"; cout << in << endl; for (int i = 1; i <= 10; i = i + 1) { in = rand(in); cout << in << endl; } return 0; }