// hw1ans by Chung #include #include class MyRandom { private: int inc; int mul; int seed; int mod; public: MyRandom(int s=1, int mu=40, int i=3641, int mo=729) // default agrs { seed = s; mul = mu; inc = i; mod = mo; } void changeSeed(int s) { seed=s; } // Ex. 11 int rand1() // Ex. 11 { seed = (mul*seed+inc)%mod; return seed; } double rand2() // Ex. 12 { seed = (mul*seed+inc)%mod; return (double)seed/mod; } }; void prttab(MyRandom& r) // Ex. 13 { int cnt[10]={0,0,0,0,0,0,0,0,0,0}; double tmp; for (int i=0;i<1000000;i++) { tmp=r.rand2(); for(int j=0;j<10;j++) { if(tmp<(j/10.0)+0.1 && tmp>=j/10.0) cnt[j]++; } } cout << "Range" << setw(25) << "# of Occrrences" << endl; cout.setf(ios::fixed); cout.setf(ios::showpoint); cout.precision(1); for(int i=0;i<10;i++) { double t = i/10.0; cout << "["<< t << ".." << (t+0.1) <<")"; cout << setw(15) << cnt[i] << endl; } } void main () { MyRandom test1; cout << "Test1 using default arguments ---------------------\n"; prttab(test1); MyRandom test2(1,37,4671,875); cout << "Test2 using 1,37,4671,875 -------------------------\n"; prttab(test2); }