#ifndef RAND_H #define RAND_H /////////////////////////////////////////////////////////////// // rand.h - 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 // /////////////////////////////////////////////////////////////// /* Class Operations: ================= The rand class objects generate uniformily distributed integers or doubles in an interval specified in the generator's constructor call. Some care has been taken to make each new object generate percep- tively different number sequences. However, since the random number generator seed is derived from system time, constructions which lie within a millisecond of each other will have nearly the same values. For this reason it is advisable to make the random number generator object a static variable. Thus it will only be constructed once and will generate unique numbers each time called up to the period of the underlying random number generator provided by cstdlib::rand() function. Public Interface: ================= uniform gi(0,99); constructs a generator of ints for [0,99] uniform gd(0.0,1.0); constructs a generator of doubles for [0.0,1.0] void seed(long int s); allows user to supply a fixed seed so that the same sequence results every time the program is run double d = gd.gen(); generates a random double value if(event(p)) generate a true value with probability p { stuff } */ // /////////////////////////////////////////////////////////////// // Build Process // /////////////////////////////////////////////////////////////// // Required Files: // // rand.h, rand.cpp // // // // Compiler Command: // // cl /GX /DTEST_RAND rand.cpp // /////////////////////////////////////////////////////////////// /* Maintenance History: ==================== ver 1.0 : 20 Jul 99 - first release */ #include #include #include #include template class uniform { public: uniform(T min, T max); void seed(long int s); T gen(); private: double scaleFactor; T _max,_min; }; bool event(double prob); // //----< constructor >------------------------------------------ template uniform::uniform(T min, T max) : _min(min), _max(max) { scaleFactor = double (max-min)/double(RAND_MAX); // set range of values // Calculate seed: // - need to use large seed value for random number generator in order to // get a relatively long period between repeating values. // - need to generate quite different seed each time constructor is called // so numbers will be peceptually different for each new generator. We // accomplish this by multiplying the relatively large count value by // millisecond count of the system clock. unsigned int count = 654321; SYSTEMTIME st; GetSystemTime(&st); count *= st.wMilliseconds; // seed the random number generator using the volitile count and the // system time which varies slowly, but with fine resolution, since // the time function returns the number of seconds elapsed since midnight // Jan 1, 1970. srand((unsigned)time(NULL) + count); } //----< give random number generator a specified seed >-------- template void uniform::seed(long int s) { unsigned int count = 654321; srand(s+count); } //----< generate a uniformily distributed random number >------ template T uniform::gen() { double d = static_cast(rand())*scaleFactor; return static_cast(d + _min); } //----< generate an event with given probability >------------- bool event(double probability) { static uniform value(0.0, 1.0); double val = value.gen(); if(value.gen() <= probability) return true; return false; } #endif