/////////////////////////////////////////////////////////////// // rand.cpp - generate uniformily distributed random // // ver 1.0 numbers in some interval. Template class // // generates either int or double numbers. // // // // Langauge: Visual C++, ver 6.0 // // Platform: Micron Dual Pentium Pro 200, Win NT 4.0 // // Application: ELE591/791 Project #2 prototype // // Author: Jim Fawcett, CST 2-187, Syracuse Univ. // // (315) 443-3948, fawcett@ecs.syr.edu // /////////////////////////////////////////////////////////////// //----< force seeding >---------------------------------------- #include "stdafx.h" #include "rand.h" //Seed uniform::_s; //Seed uniform::_s; //----< test stub >-------------------------------------------- #ifdef TEST_RAND #include #include using namespace std; void main() { cout << "\n Testing Uniform Random Number Generator " << "\n =========================================\n\n"; cout << "\n generating random integers in [1,100] " << "\n ---------------------------------------\n\n"; uniform r1(1,100); int i,j; for(i=0; i<5; i++) { for(j=0; j<20; j++) cout << setw(3) << r1.gen(); cout << '\n'; } cout << "\n\n"; cout << "\n generating random doubles in [0,1] " << "\n ---------------------------------------\n\n"; uniform r2(0.0,1.0); cout.setf(ios::floatfield,ios::fixed); cout.setf(ios::showpoint); for(i=0; i<5; i++) { for(j=0; j<7; j++) cout << setw(10) << r2.gen(); cout << '\n'; } cout << "\n\n"; cout << "\n generating random events with probability 0.75 " << "\n ------------------------------------------------\n\n"; const int maxCount = 10000; int count = 0; for(i=0; i r1a(1,5); int histCnt[7] = { 0,0,0,0,0,0,0 }; for(i=0; i<1000000; i++) { int rnd = r1a.gen(); switch(rnd) { case 0 : histCnt[0]++; break; case 1 : histCnt[1]++; break; case 2 : histCnt[2]++; break; case 3 : histCnt[3]++; break; case 4 : histCnt[4]++; break; case 5 : histCnt[5]++; break; case 6 : histCnt[6]++; break; } } cout << "\n histCnt: " << histCnt[0] << ", " << histCnt[1] << ", " << histCnt[2] << ", " << histCnt[3] << ", " << histCnt[4] << ", " << histCnt[5] << ", " << histCnt[6] << endl; cout << "\n\n"; cout << "\n generating histogram of random doubles in [1.0, 5.0] " << "\n ------------------------------------------------------\n\n"; uniform r1b(1.0,5.0); double dhistCnt[7] = { 0,0,0,0,0,0,0 }; for(i=0; i<1000000; i++) { double drand = r1b.gen(); if(drand < 1) // in [0,1) dhistCnt[0]++; else if(drand < 2) // in [1,2) dhistCnt[1]++; else if(drand < 3) // in [2,3) dhistCnt[2]++; else if(drand < 4) // in [3,4) dhistCnt[3]++; else if(drand <= 5) // in [4,5] NOTE INEQUALITY !! dhistCnt[4]++; else if(drand < 6) // in (5,6) dhistCnt[5]++; } cout.precision(0); cout << "\n histCnt: " << dhistCnt[0] << ", " << dhistCnt[1] << ", " << dhistCnt[2] << ", " << dhistCnt[3] << ", " << dhistCnt[4] << ", " << dhistCnt[5] << ", " << dhistCnt[6] << endl; cout << "\n\n"; cout << "\n generating repeatable random ints in [1,50] using seed() " << "\n ----------------------------------------------------------\n\n"; uniform r1c(1,50); r1c.seed(); for(i=0; i<5; i++) { for(j=0; j<20; j++) cout << setw(3) << r1c.gen(); cout << '\n'; } cout << "\n\n"; r1c.seed(); for(i=0; i<5; i++) { for(j=0; j<20; j++) cout << setw(3) << r1c.gen(); cout << '\n'; } cout << "\n\n"; } #endif