FILENAME: example9.cpp
#include <rw/math/funcobj.h> #include <rw/histo.h> #include <rw/rstream.h> #include <rw/rand.h> #include "myrand.h" //********************************************************** // Class MyRand demonstrates how to convert a function into a // function object, which can then be used as the template // parameter to the random number generator classes in the // Essential Math Module. // The function motherRNG is George Marsaglia's "Mother of all // Random Number Generators" and is defined in mrng.cpp. //********************************************************** class MyRand { public: // RandFtn is a typedef for a pointer to a // function that takes an unsigned long* and returns a double: typedef double (*RandFtn)( unsigned long * ); // Function call operator returns the random number // by calling the function: double operator()() { return (*randFunction_)(&randInt_); } // Default constructor: MyRand(unsigned long seed = 123455):randFunction_(motherRNG), randInt_(seed) {;} private: unsigned long randInt_; RandFtn randFunction_; }; int main() { // Create a binomial random number generator // using the class MyRand to generate the // underlying uniform (0,1) deviates: RWTRandBinomial<MyRand> randBino( 23 ); // Create a thousand random numbers and store them in a vector. RWMathVec<double> randomNumbers( 1000, rwUninitialized ); RWMathVec<double>::iterator first = randomNumbers.begin(); RWMathVec<double>::iterator last = randomNumbers.end(); while (first != last) *first++ = randBino(); // Put the random numbers in a histogram and print them. Histogram h( 10U, randomNumbers ); std::cout << h << std::endl; return 0; } |
Sample Input:
None required. |
Sample Output:
HISTOGRAM: Total number of Counts = 1000 Total number of bins = 10 Number of larger values = 0 Number of smaller values = 0 Bin number -- Bin boundaries -- Counts 0 2 3.2 54 1 3.2 4.4 89 2 4.4 5.6 123 3 5.6 6.8 163 4 6.8 8 368 5 8 9.2 97 6 9.2 10.4 53 7 10.4 11.6 35 8 11.6 12.8 10 9 12.8 14 8 |
Copyright © Rogue Wave Software, Inc. All Rights Reserved.
The Rogue Wave name and logo, and SourcePro, are registered trademarks of Rogue Wave Software. All other trademarks are the property of their respective owners.
Provide feedback to Rogue Wave about its documentation.