|
STK++ 1.0
|
class for the Base random generator. More...
#include <STK_RandBase.h>

Public Member Functions | |
| RandBase (const Real &glimit=3.442619855899, const Real &gvol=9.91256303526217e-3, Integer const &gsize=128) | |
| Default constructor auto-initialize with /dev/urandom or time() and clock(). | |
| RandBase (Integer const &oneSeed, const Real &glimit=3.442619855899, const Real &gvol=9.91256303526217e-3, Integer const &gsize=128) | |
| Initialize with a simple Integer seed. | |
| template<class TContainer1D > | |
| RandBase (const ITContainer1D< Integer, TContainer1D > &bigSeed, const Real &glimit=3.442619855899, const Real &gvol=9.91256303526217e-3, Integer const &gsize=128) | |
| Initialize with an Integer seed Array. | |
| ~RandBase () | |
| destructor. | |
| Integer | randDiscreteUnif () |
| Pseudo-random Integer uniform generator. | |
| Real | randUnif () |
| pseudo-random uniform generator. | |
| Real | operator() () |
| same as randUnif(). | |
| Real | randGauss (Real const &mu=0, Real const &sigma=1) |
| Pseudo-random gaussian generator of the gaussian probability law:
| |
| Real | randExp () |
| Pseudo-random exponential generator. | |
| template<class TContainer1D > | |
| void | randDiscreteUnif (ITContainer1D< Integer, TContainer1D > &A) |
| Pseudo-random Integer uniform generator for a uni-dimensional container of Integer . | |
| template<class TContainer1D > | |
| void | randUnif (ITContainer1D< Real, TContainer1D > &A) |
| Pseudo-random Real uniform generator for a uni-dimensional container of Real. | |
| template<class TContainer1D > | |
| void | randGauss (ITContainer1D< Real, TContainer1D > &A) |
| Pseudo-random Real gaussian generator for a uni-dimensional container of Real. | |
| template<class TContainer1D > | |
| void | randExp (ITContainer1D< Real, TContainer1D > &A) |
| Pseudo-random Real exponential generator for a uni-dimensional container of Real. | |
Private Member Functions | |
| void | gaussInit () |
| Initialization of the Zigourrat method. | |
Private Attributes | |
| const Integer | gsize_ |
| Number of box for the gaussian ziggourat method. | |
| const Real | glimit_ |
| limit of the bottom box. | |
| const Real | gvol_ |
| volum of each box and of the remaining tail. | |
| Real * | kn |
| kn holds coordinates, such that each rectangle has same area. | |
| Real * | wn |
| Real * | fn |
class for the Base random generator.
This class inherit from MTRand whitch should not be used directly. Using RandBase, one get a Type safe generator for use in STK applications.
This class furnish :
For the exponential Law we remove the old method and use directly the inverse pdf method.
Definition at line 75 of file STK_RandBase.h.
| STK::RandBase::RandBase | ( | const Real & | glimit = 3.442619855899, |
| const Real & | gvol = 9.91256303526217e-3, |
||
| Integer const & | gsize = 128 |
||
| ) |
Default constructor auto-initialize with /dev/urandom or time() and clock().
| glimit | maximal value of the boxes in the ziggourat method |
| gvol | volume of each box in the ziggourat method |
| gsize | number of boxes |
Definition at line 55 of file STK_RandBase.cpp.
References gaussInit().
| STK::RandBase::RandBase | ( | Integer const & | oneSeed, |
| const Real & | glimit = 3.442619855899, |
||
| const Real & | gvol = 9.91256303526217e-3, |
||
| Integer const & | gsize = 128 |
||
| ) |
Initialize with a simple Integer seed.
| oneSeed | seed of the generator |
| glimit | maximal value of the boxes in the ziggourat method |
| gvol | volume of each box in the ziggourat method |
| gsize | number of boxes |
Definition at line 41 of file STK_RandBase.cpp.
References gaussInit().
| STK::RandBase::RandBase | ( | const ITContainer1D< Integer, TContainer1D > & | bigSeed, |
| const Real & | glimit = 3.442619855899, |
||
| const Real & | gvol = 9.91256303526217e-3, |
||
| Integer const & | gsize = 128 |
||
| ) | [inline] |
Initialize with an Integer seed Array.
| bigSeed | seed of the generator |
| glimit | maximal value of the boxes in the ziggourat method |
| gvol | volume of each box in the ziggourat method |
| gsize | number of boxes |
Definition at line 129 of file STK_RandBase.h.
References STK::ITContainer1D< TYPE, TContainer1D >::first(), gaussInit(), STK::ITContainer1D< TYPE, TContainer1D >::last(), MTRand::seed(), and STK::ITContainer1D< TYPE, TContainer1D >::size().
: gsize_(gsize) , glimit_(glimit) , gvol_(gvol) { // dimension Integer first = bigSeed.first(), last = bigSeed.last() , size = bigSeed.size(); uint32 arraySeed[size]; // cast Integer in uint32 for (Integer i=first; i<=last; i++) { arraySeed[i-first] = uint32(bigSeed[i]); } // Re-seeding functions with same behavior as initializers seed(arraySeed, size); // initialize zigourat method for gaussian random generator gaussInit(); }
| STK::RandBase::~RandBase | ( | ) |
| Integer STK::RandBase::randDiscreteUnif | ( | ) | [inline] |
Pseudo-random Integer uniform generator.
Return a [0,n] uniform integer number for n < 2^32 using the Mersenne Twister method. This is a wrapper of the MTRand class.
Definition at line 164 of file STK_RandBase.h.
References MTRand::randInt().
Referenced by randDiscreteUnif().
| Real STK::RandBase::randUnif | ( | ) | [inline] |
pseudo-random uniform generator.
Return a (0,1) uniform number using the Mersenne Twister method. This is a wrapper of the MTRand class.
Definition at line 175 of file STK_RandBase.h.
References MTRand::randDblExc().
Referenced by operator()(), STK::Law::Cauchy::rand(), randExp(), randGauss(), and randUnif().
{ return Real(randDblExc());}
| Real STK::RandBase::operator() | ( | ) | [inline] |
same as randUnif().
Reimplemented from MTRand.
Definition at line 179 of file STK_RandBase.h.
References randUnif().
{ return randUnif(); }
Pseudo-random gaussian generator of the gaussian probability law:
.
| mu | mean of the gaussian distribution |
| sigma | standard deviation of the gaussian distribution |
Definition at line 82 of file STK_RandBase.cpp.
References STK::abs(), fn, glimit_, kn, randExp(), MTRand::randInt(), randUnif(), STK::sign(), and wn.
Referenced by STK::Law::Normal::rand(), and randGauss().
{
while(1)
{
// uniforms number
Real u = 2.0 * randUnif() - 1.0;
// random box
uint32 i = randInt() & 0x7F; //
// squeeze step : try the rectangular boxes
if (abs(u) < wn[i]) return mu + sigma * u * kn[i];
// bottom box: we have to sample from the tail
if (i == 0)
{
// result
Real x;
// loop
do { x = randExp() / glimit_;}
while ( 2.0 * randExp() < x * x);
// result sign(u) * (x+dr)
return mu + sigma * sign(u, x+glimit_);
}
else // other box
{ // x : result, f1 : intermediary result
Real x = u * kn[i], f1 = fn[i+1];
// reject step : is this a sample from the wedges ?
if ( f1 + randUnif()*( fn[i]*exp(x*x/2.)-f1) < 1.0)
{ return mu + sigma * x;}
}
}
// avoid warning at compilation
return 0.;
}
| Real STK::RandBase::randExp | ( | ) |
Pseudo-random exponential generator.
Return a real number from an exponential normalized distribution using the inverse pdf method.
Definition at line 122 of file STK_RandBase.cpp.
References randUnif().
Referenced by randExp(), and randGauss().
{
return -log(1.-randUnif());
}
| void STK::RandBase::randDiscreteUnif | ( | ITContainer1D< Integer, TContainer1D > & | A | ) | [inline] |
Pseudo-random Integer uniform generator for a uni-dimensional container of Integer .
| A | container to fill |
Definition at line 206 of file STK_RandBase.h.
References STK::ITContainer1D< TYPE, TContainer1D >::first(), STK::ITContainer1D< TYPE, TContainer1D >::last(), and randDiscreteUnif().
{ for (Integer i=A.first(); i<=A.last(); i++)
A[i] = randDiscreteUnif();
}
| void STK::RandBase::randUnif | ( | ITContainer1D< Real, TContainer1D > & | A | ) | [inline] |
Pseudo-random Real uniform generator for a uni-dimensional container of Real.
| A | container to fill |
Definition at line 216 of file STK_RandBase.h.
References STK::ITContainer1D< TYPE, TContainer1D >::first(), STK::ITContainer1D< TYPE, TContainer1D >::last(), and randUnif().
| void STK::RandBase::randGauss | ( | ITContainer1D< Real, TContainer1D > & | A | ) | [inline] |
Pseudo-random Real gaussian generator for a uni-dimensional container of Real.
| A | container to fill |
Definition at line 226 of file STK_RandBase.h.
References STK::ITContainer1D< TYPE, TContainer1D >::first(), STK::ITContainer1D< TYPE, TContainer1D >::last(), and randGauss().
| void STK::RandBase::randExp | ( | ITContainer1D< Real, TContainer1D > & | A | ) | [inline] |
Pseudo-random Real exponential generator for a uni-dimensional container of Real.
| A | container to fill |
Definition at line 236 of file STK_RandBase.h.
References STK::ITContainer1D< TYPE, TContainer1D >::first(), STK::ITContainer1D< TYPE, TContainer1D >::last(), and randExp().
| void STK::RandBase::gaussInit | ( | ) | [private] |
Initialization of the Zigourrat method.
Definition at line 128 of file STK_RandBase.cpp.
References fn, glimit_, gsize_, gvol_, kn, and wn.
Referenced by RandBase().
{
kn = new Real[gsize_+1];
wn = new Real[gsize_];
fn = new Real[gsize_+1];
Real f = exp(-0.5 * glimit_ * glimit_);
kn[0] = gvol_ / f; // [0] is bottom block: gvol/f(gbound)
kn[gsize_] = 0;
fn[gsize_] = 1;
kn[1] = glimit_;
fn[1] = f;
for (Integer i = 2; i < gsize_; ++i)
{
kn[i] = sqrt(-2 * log(gvol_ / kn[i-1] + f));
fn[i] = (f = exp(-0.5 * kn[i] * kn[i]));
}
for (Integer i = 0; i < gsize_; ++i)
wn[i] = kn[i + 1] / kn[i];
}
const Integer STK::RandBase::gsize_ [private] |
Number of box for the gaussian ziggourat method.
[0] is bottom box and [size_-1] is top box.
Definition at line 83 of file STK_RandBase.h.
Referenced by gaussInit().
const Real STK::RandBase::glimit_ [private] |
limit of the bottom box.
Definition at line 86 of file STK_RandBase.h.
Referenced by gaussInit(), and randGauss().
const Real STK::RandBase::gvol_ [private] |
volum of each box and of the remaining tail.
Definition at line 89 of file STK_RandBase.h.
Referenced by gaussInit().
Real* STK::RandBase::kn [private] |
kn holds coordinates, such that each rectangle has same area.
wn holds kn[i+1]/kn[i]. fn holds exp(-0.5 * kn[i] * kn[i]).
Definition at line 95 of file STK_RandBase.h.
Referenced by gaussInit(), randGauss(), and ~RandBase().
Real * STK::RandBase::wn [private] |
Definition at line 95 of file STK_RandBase.h.
Referenced by gaussInit(), randGauss(), and ~RandBase().
Real * STK::RandBase::fn [private] |
Definition at line 95 of file STK_RandBase.h.
Referenced by gaussInit(), randGauss(), and ~RandBase().