STK++ 1.0

STK::RandBase Class Reference

class for the Base random generator. More...

#include <STK_RandBase.h>

Inheritance diagram for STK::RandBase:

List of all members.

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:

\[ f(x) = \frac{1}{\sqrt{2\pi}} \exp\left\{-\frac{x^2}{2}\right\}. \]

.

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.
Realkn
 kn holds coordinates, such that each rectangle has same area.
Realwn
Realfn

Detailed Description

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 :

  • A pseudo normalized uniform random variate generator
  • A pseudo normalized gaussian random variate generator
  • A pseudo normalized exponential random variate generator
Author:
George Marsaglia andWai Wan Tsang, "The Ziggurat Method for Generating Random Variables", Journal of Statistical Software, Volume 5, 2000, Issue 8.
Jurgen A Doornik, "An improved Ziggurat Method to generate Normal Random Sample" http://www.doornik.com/research.html (2005).

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.


Constructor & Destructor Documentation

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().

Parameters:
glimitmaximal value of the boxes in the ziggourat method
gvolvolume of each box in the ziggourat method
gsizenumber of boxes

Definition at line 55 of file STK_RandBase.cpp.

References gaussInit().

                  : MTRand()
                  , gsize_(gsize)
                  , glimit_(glimit)
                  , gvol_(gvol)
{
  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.

Parameters:
oneSeedseed of the generator
glimitmaximal value of the boxes in the ziggourat method
gvolvolume of each box in the ziggourat method
gsizenumber of boxes

Definition at line 41 of file STK_RandBase.cpp.

References gaussInit().

                  : MTRand( (uint32)oneSeed )
                  , gsize_(gsize)
                  , glimit_(glimit)
                  , gvol_(gvol)
{
  gaussInit();
}
template<class TContainer1D >
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.

Parameters:
bigSeedseed of the generator
glimitmaximal value of the boxes in the ziggourat method
gvolvolume of each box in the ziggourat method
gsizenumber 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 ( )

destructor.

Definition at line 68 of file STK_RandBase.cpp.

References fn, kn, and wn.

{
  delete [] kn;
  delete [] wn;
  delete [] fn;
}

Member Function Documentation

Integer STK::RandBase::randDiscreteUnif ( ) [inline]

Pseudo-random Integer uniform generator.

\[ f(x) = 1/(n+1), \quad 0\leq x \leq n \]

Return a [0,n] uniform integer number for n < 2^32 using the Mersenne Twister method. This is a wrapper of the MTRand class.

See also:
STK::MTRand

Definition at line 164 of file STK_RandBase.h.

References MTRand::randInt().

Referenced by randDiscreteUnif().

    { return Integer(randInt());}
Real STK::RandBase::randUnif ( ) [inline]

pseudo-random uniform generator.

\[ f(x) = 1, \qquad 0< x <1 \]

Return a (0,1) uniform number using the Mersenne Twister method. This is a wrapper of the MTRand class.

See also:
STK::MTRand

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(); }
Real STK::RandBase::randGauss ( Real const &  mu = 0,
Real const &  sigma = 1 
)

Pseudo-random gaussian generator of the gaussian probability law:

\[ f(x) = \frac{1}{\sqrt{2\pi}} \exp\left\{-\frac{x^2}{2}\right\}. \]

.

Parameters:
mumean of the gaussian distribution
sigmastandard deviation of the gaussian distribution
Returns:
a real number from a normal (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.

\[ f(x) = \exp\left\{ -x \right\}. \]

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());
}
template<class TContainer1D >
void STK::RandBase::randDiscreteUnif ( ITContainer1D< Integer, TContainer1D > &  A) [inline]

Pseudo-random Integer uniform generator for a uni-dimensional container of Integer .

Parameters:
Acontainer 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();
    }
template<class TContainer1D >
void STK::RandBase::randUnif ( ITContainer1D< Real, TContainer1D > &  A) [inline]

Pseudo-random Real uniform generator for a uni-dimensional container of Real.

Parameters:
Acontainer to fill

Definition at line 216 of file STK_RandBase.h.

References STK::ITContainer1D< TYPE, TContainer1D >::first(), STK::ITContainer1D< TYPE, TContainer1D >::last(), and randUnif().

    { for (Integer i=A.first(); i<=A.last(); i++)
        A[i] = randUnif();
    }
template<class TContainer1D >
void STK::RandBase::randGauss ( ITContainer1D< Real, TContainer1D > &  A) [inline]

Pseudo-random Real gaussian generator for a uni-dimensional container of Real.

Parameters:
Acontainer to fill

Definition at line 226 of file STK_RandBase.h.

References STK::ITContainer1D< TYPE, TContainer1D >::first(), STK::ITContainer1D< TYPE, TContainer1D >::last(), and randGauss().

    { for (Integer i=A.first(); i<=A.last(); i++)
        A[i] = randGauss();
    }
template<class TContainer1D >
void STK::RandBase::randExp ( ITContainer1D< Real, TContainer1D > &  A) [inline]

Pseudo-random Real exponential generator for a uni-dimensional container of Real.

Parameters:
Acontainer to fill

Definition at line 236 of file STK_RandBase.h.

References STK::ITContainer1D< TYPE, TContainer1D >::first(), STK::ITContainer1D< TYPE, TContainer1D >::last(), and randExp().

    { for (Integer i=A.first(); i<=A.last(); i++)
        A[i] = 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];
}

Member Data Documentation

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().


The documentation for this class was generated from the following files: