/************************************************************************************* *** FILE: random.cpp *** DESC: This program is from Robert Sedgewick "Algorithms in C++" book. *************************************************************************************/ #include using namespace std; #define square(a) (a)*(a) class Random { public: Random() { a = 2498721; } Random(int seed) { a = seed; }; int next(); int next(int r); int multiply(int p, int q); private: int a; enum { M1=10000, M=M1*M1, b=31415821 }; }; int Random::next() { a = (multiply(a, b) + 1) % M; return a; } int Random::next(int r) { return ((next()/M1)*r)/M1; } int Random::multiply(int p, int q) { int p1, p0, q1, q0; p1 = p / M1; p0 = p % M1; q1 = q / M1; q0 = q % M1; return (((p0*q1+p1*q0)%M1)*M1 + p0*q0) % M; } void main(int argc, char *argv[]) { Random r(2718281); for(int i=0;i<20;i++) cout<