Module: Essential Math Group: Statistics classes
RWTRandNormal<Generator>RWTRand<Generator>
#include <rw/rand.h> RWTRandNormal<Generator> gen;
Class RWTRandNormal<Generator> generates random numbers from a normal distribution:
where µ is the mean, and 2 is the variance. The algorithm used to generate the random numbers begins with random numbers generated from a uniform distribution on the interval [0, 1]. Thus, the speed and statistical properties of the normal random numbers may be varied by varying the Generator template parameter, which is responsible for generating these uniformly distributed numbers.
The class Generator must be a function object whose function call operator returns a uniform random double between 0 and 1. The class RWRandGenerator supplied with the library may be used.
#include <rw/rand.h> #include <iostream.h> #include "myrand.h" int main() { // Create a uniform generator on [0, 1] with an
// initial seed value. RWRandGenerator uniform(123456L); // Create a normal generator with mean 75 and variance 100 // using the above uniform generator for generator. RWTRandNormal<RWRandGenerator> normal(uniform, 75.0, 100.0); // Create a normal generator with mean 0 and variance 1
// using class RWRandGenerator. RWTRandNormal<RWRandGenerator> normalZeroOne; // Create a normal generator with mean -3.14 and variance // 345.67 using a class MyDoubleRand to generate the
// uniform [0, 1] deviates. RWTRandNormal<MyDoubleRand> myRandNormal( -3.14, 345.67 ); // Print a few values. for ( int j = 0; j < 10; j++ ) { cout << "normal(75,100) = " << normal(); cout << "\normal(0,1) = " << normalZeroOne(); cout << "normal(-3.14,345.67) = " << myRandNormal() << endl; } // Restart one of the generators with another seed value // using the RWRandGenerator method restart(). ( normalZeroOne.generator() ).restart( 654321L); return 0; }
RWTRandNormal();
Constructs a generator with mean 0 and variance 1.
RWTRandNormal(double mean, double variance);
Constructs a generator with the mean specified by the first parameter and variance specified by the second parameter.
RWTRandNormal(const Generator& g, double mean=0, variance=1);
Constructs a generator with underlying uniform [0, 1] generator g and the specified mean and variance.
RWTRandNormal(const RWTRandNormal<Generator>& g);
Constructs self as a copy of g.
double operator()()
Returns the next random number in the sequence.
size_t operator()(double* out, size_t n);
Assigns random numbers to n elements in out. Returns the number of random numbers generated.
double mean() const;
Returns the mean of the distribution.
void setMean(double m);
Sets the mean of the distribution to m.
void setVariance(double v);
Sets the variance of the distribution to v.
double variance() const;
Returns the variance of the distribution.
© Copyright Rogue Wave Software, Inc. All Rights Reserved.
Rogue Wave and SourcePro are registered trademarks of Rogue Wave Software, Inc. in the United States and other countries. All other trademarks are the property of their respective owners.
Contact Rogue Wave about documentation or support issues.