STK++ 0.9.13

class for the Base random generator. More...

#include <STK_RandBase.h>

Inheritance diagram for STK::RandBase:
Inheritance graph

Public Member Functions

 RandBase (Real const &glimit=3.442619855899, Real const &gvol=9.91256303526217e-3, int gsize=128)
 Default constructor.
 
 RandBase (int oneSeed, Real const &glimit=3.442619855899, Real const &gvol=9.91256303526217e-3, int gsize=128)
 Initialize with a simple int seed.
 
template<class TContainer1D >
 RandBase (TContainer1D const &bigSeed, Real const &glimit=3.442619855899, Real const &gvol=9.91256303526217e-3, int gsize=128)
 Initialize with a seed Array.
 
 ~RandBase ()
 destructor.
 
int randDiscreteUnif ()
 Pseudo-random int uniform generator.
 
Real randUnif ()
 pseudo-random uniform generator.
 
Real operator() ()
 
Real rand ()
 real number in [0,1]
 
Real rand (Real const &n)
 real number in [0,n]
 
Real randExc ()
 real number in [0,1)
 
Real randExc (Real const &n)
 real number in [0,n)
 
Real randDblExc ()
 real number in (0,1)
 
Real randDblExc (Real const &n)
 real number in (0,n)
 
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.
 

Private Member Functions

void gaussInit ()
 Initialization of the Zigourrat method.
 

Private Attributes

const int gsize_
 Number of box for the gaussian ziggourat method.
 
Real const glimit_
 limit of the bottom box.
 
Real const gvol_
 volume of each box and of the remaining tail.
 
Realkn
 kn holds coordinates, such that each rectangle has same area.
 
Realwn
 
Realfn
 

Additional Inherited Members

- Protected Types inherited from MTRand
enum  { M = 397 }
 
enum  { N = 624 }
 
enum  { SAVE = N + 1 }
 
typedef unsigned long uint32
 unsigned integer type, at least 32 bits
 
- Protected Member Functions inherited from MTRand
void initialize (const uint32 seed)
 Initialize generator state with seed See Knuth TAOCP Vol 2, 3rd Ed, p.106 for multiplier.
 
void reload ()
 Generate N new values in state Made disposeer and faster by Matthew Bellew (matth.nosp@m.ew.b.nosp@m.ellew.nosp@m.@hom.nosp@m.e.com)
 
uint32 hiBit (const uint32 &u) const
 High bit.
 
uint32 loBit (const uint32 &u) const
 low bit.
 
uint32 loBits (const uint32 &u) const
 low bits.
 
uint32 mixBits (const uint32 &u, const uint32 &v) const
 mixed bits.
 
uint32 twist (const uint32 &m, const uint32 &s0, const uint32 &s1) const
 twisted values.
 
 MTRand (const uint32 &oneSeed)
 Initialize with a simple uint32.
 
 MTRand (uint32 *const bigSeed, uint32 const seedLength=N)
 Initialize with a an array.
 
 MTRand ()
 auto-initialize with /dev/urandom or time() and clock()
 
double rand ()
 real number in [0,1]
 
double rand (const double &n)
 real number in [0,n]
 
double randExc ()
 real number in [0,1)
 
double randExc (const double &n)
 real number in [0,n)
 
double randDblExc ()
 real number in (0,1)
 
double randDblExc (const double &n)
 real number in (0,n)
 
uint32 randInt ()
 integer in [0,2^32-1]
 
double operator() ()
 same as rand().
 
uint32 randInt (const uint32 &n)
 integer in [0,n] for n < 2^32
 
double rand53 ()
 Access to 53-bit random numbers (capacity of IEEE double precision).
 
double randNorm (const double &mean, const double &variance)
 Access to nonuniform random number distributions.
 
void seed (const uint32 oneSeed)
 Re-seeding functions with same behavior as initializers.
 
void seed (uint32 *const bigSeed, const uint32 seedLength)
 Re-seeding functions with same behavior as initializers.
 
void seed ()
 Re-seeding functions with same behavior as initializers.
 
void save (uint32 *saveArray) const
 Saving and loading generator state.
 
void load (uint32 *const loadArray)
 Saving and loading generator state.
 
- Static Protected Member Functions inherited from MTRand
static uint32 hash (time_t t, clock_t c)
 Get a uint32 from t and c Better than uint32(x) in case x is floating point in [0,1] Based on code by Lawrence Kirby (fred@.nosp@m.gene.nosp@m.sis.d.nosp@m.emon.nosp@m..co.u.nosp@m.k)
 
- Protected Attributes inherited from MTRand
uint32 state [N]
 internal state
 
uint32pNext
 Next value to get from state.
 
int left
 number of values left before reload needed
 

Detailed Description

class for the Base random generator.

This class inherit from MTRand which 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 131 of file STK_RandBase.h.

Constructor & Destructor Documentation

◆ RandBase() [1/3]

STK::RandBase::RandBase ( Real const glimit = 3.442619855899,
Real const gvol = 9.91256303526217e-3,
int  gsize = 128 
)
inline

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 262 of file STK_RandBase.h.

263 : MTRand(), gsize_(gsize), glimit_(glimit), gvol_(gvol)
264{ gaussInit();}
MTRand()
auto-initialize with /dev/urandom or time() and clock()
Real const glimit_
limit of the bottom box.
const int gsize_
Number of box for the gaussian ziggourat method.
Real const gvol_
volume of each box and of the remaining tail.
void gaussInit()
Initialization of the Zigourrat method.

References gaussInit().

◆ RandBase() [2/3]

STK::RandBase::RandBase ( int  oneSeed,
Real const glimit = 3.442619855899,
Real const gvol = 9.91256303526217e-3,
int  gsize = 128 
)
inline

Initialize with a simple int 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 257 of file STK_RandBase.h.

258 : MTRand( (uint32)oneSeed ), gsize_(gsize), glimit_(glimit), gvol_(gvol)
259{ gaussInit();}
unsigned long uint32
unsigned integer type, at least 32 bits

References gaussInit().

◆ RandBase() [3/3]

template<class TContainer1D >
STK::RandBase::RandBase ( TContainer1D const bigSeed,
Real const glimit = 3.442619855899,
Real const gvol = 9.91256303526217e-3,
int  gsize = 128 
)
inline

Initialize with a 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 163 of file STK_RandBase.h.

168 : gsize_(gsize), glimit_(glimit), gvol_(gvol)
169 {
170 // dimension
171 int first = bigSeed.begin(), size = bigSeed.size();
172 uint32* arraySeed = new uint32[size];
173 // cast int in uint32
174 for (int i=first; i<bigSeed.end(); i++)
175 { arraySeed[i-first] = uint32(bigSeed[i]);}
176 // Re-seeding functions with same behavior as initializers
177 seed(arraySeed, size);
178 delete[] arraySeed;
179 // initialize zigourat method for gaussian random generator
180 gaussInit();
181 }
void seed()
Re-seeding functions with same behavior as initializers.

References gaussInit(), and MTRand::seed().

◆ ~RandBase()

STK::RandBase::~RandBase ( )
inline

destructor.

Definition at line 267 of file STK_RandBase.h.

268{
269 delete [] kn;
270 delete [] wn;
271 delete [] fn;
272}
Real * kn
kn holds coordinates, such that each rectangle has same area.

References fn, kn, and wn.

Member Function Documentation

◆ gaussInit()

void STK::RandBase::gaussInit ( )
inlineprivate

Initialization of the Zigourrat method.

Definition at line 324 of file STK_RandBase.h.

325{
326 kn = new Real[gsize_+1];
327 wn = new Real[gsize_];
328 fn = new Real[gsize_+1];
329
330 Real f = exp(-0.5 * glimit_ * glimit_);
331 kn[0] = gvol_ / f; // [0] is bottom block: gvol/f(gbound)
332
333 kn[gsize_] = 0;
334 fn[gsize_] = 1;
335
336 kn[1] = glimit_;
337 fn[1] = f;
338
339 for (int i = 2; i < gsize_; ++i)
340 {
341 kn[i] = sqrt(-2 * log(gvol_ / kn[i-1] + f));
342 fn[i] = (f = exp(-0.5 * kn[i] * kn[i]));
343 }
344 for (int i = 0; i < gsize_; ++i)
345 wn[i] = kn[i + 1] / kn[i];
346}
double Real
STK fundamental type of Real values.

References fn, glimit_, gsize_, gvol_, kn, and wn.

Referenced by RandBase(), RandBase(), and RandBase().

◆ operator()()

Real STK::RandBase::operator() ( )
inline
Returns
same as randUnif().

Definition at line 204 of file STK_RandBase.h.

204{ return (Real)MTRand::operator()(); }
double operator()()
same as rand().

References MTRand::operator()().

◆ rand() [1/2]

Real STK::RandBase::rand ( )
inline

real number in [0,1]

Definition at line 206 of file STK_RandBase.h.

206{ return (Real)MTRand::rand(); }
double rand()
real number in [0,1]

References MTRand::rand().

◆ rand() [2/2]

Real STK::RandBase::rand ( Real const n)
inline

real number in [0,n]

Definition at line 208 of file STK_RandBase.h.

208{ return (Real)MTRand::rand((double)n); }

References MTRand::rand().

◆ randDblExc() [1/2]

Real STK::RandBase::randDblExc ( )
inline

real number in (0,1)

Definition at line 214 of file STK_RandBase.h.

214{ return (Real)MTRand::randDblExc(); }
double randDblExc()
real number in (0,1)

References MTRand::randDblExc().

Referenced by randUnif().

◆ randDblExc() [2/2]

Real STK::RandBase::randDblExc ( Real const n)
inline

real number in (0,n)

Definition at line 216 of file STK_RandBase.h.

216{ return (Real)MTRand::randDblExc((double)n); }

References MTRand::randDblExc().

◆ randDiscreteUnif()

int STK::RandBase::randDiscreteUnif ( )
inline

Pseudo-random int 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 192 of file STK_RandBase.h.

192{ return int(Real(randInt()));}
uint32 randInt()
integer in [0,2^32-1]

References MTRand::randInt().

◆ randExc() [1/2]

Real STK::RandBase::randExc ( )
inline

real number in [0,1)

Definition at line 210 of file STK_RandBase.h.

210{ return (Real)MTRand::randExc(); }
double randExc()
real number in [0,1)

References MTRand::randExc().

◆ randExc() [2/2]

Real STK::RandBase::randExc ( Real const n)
inline

real number in [0,n)

Definition at line 212 of file STK_RandBase.h.

212{ return (Real)MTRand::randExc((double)n); }

References MTRand::randExc().

◆ randExp()

Real STK::RandBase::randExp ( )
inline

Pseudo-random exponential generator.

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

Returns
a real number from an exponential normalized distribution using the inverse pdf method.

Definition at line 320 of file STK_RandBase.h.

321{ return -Real(std::log(randUnif()));}
Real randUnif()
pseudo-random uniform generator.

References randUnif().

Referenced by randGauss().

◆ randGauss()

Real STK::RandBase::randGauss ( Real const mu = 0,
Real const sigma = 1 
)
inline

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 281 of file STK_RandBase.h.

282{
283 while(1)
284 {
285 // uniforms number
286 Real u = 2.0 * randUnif() - 1.0;
287 // random box
288 uint32 i = randInt() & 0x7F; //
289 // squeeze step : try the rectangular boxes
290 if (std::abs(u) < wn[i]) return mu + sigma * u * kn[i];
291 // bottom box: we have to sample from the tail
292 if (i == 0)
293 {
294 Real x;
295 // loop
296 do { x = randExp() / glimit_;}
297 while ( 2.0 * randExp() < x * x);
298 // result sign(u) * (x+dr)
299 return mu + sigma * sign(u, x+glimit_);
300 }
301 else // other box
302 { // x : result, f1 : intermediary result
303 Real x = u * kn[i], f1 = fn[i+1];
304 // reject step : is this a sample from the wedges ?
305 if ( f1 + randUnif()*( fn[i]*exp(x*x/2.)-f1) < 1.0)
306 { return mu + sigma * x;}
307 }
308 }
309 // avoid warning at compilation
310 return 0.;
311}
Real randExp()
Pseudo-random exponential generator.
Type sign(Type const &x, Type const &y=Type(1))
template sign value sign(x) * y: Type should be an integral type
Definition STK_Misc.h:53

References fn, glimit_, kn, randExp(), MTRand::randInt(), randUnif(), STK::sign(), and wn.

◆ randUnif()

Real STK::RandBase::randUnif ( )
inline

pseudo-random uniform generator.

This is a wrapper of the MTRand class.

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

Returns
a uniform number in (0,1) using the Mersenne Twister method.
See also
STK::MTRand

Definition at line 201 of file STK_RandBase.h.

201{ return Real(randDblExc());}
Real randDblExc()
real number in (0,1)

References randDblExc().

Referenced by randExp(), and randGauss().

Member Data Documentation

◆ fn

Real * STK::RandBase::fn
private

Definition at line 250 of file STK_RandBase.h.

Referenced by gaussInit(), randGauss(), and ~RandBase().

◆ glimit_

Real const STK::RandBase::glimit_
private

limit of the bottom box.


Definition at line 243 of file STK_RandBase.h.

Referenced by gaussInit(), and randGauss().

◆ gsize_

const int 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 241 of file STK_RandBase.h.

Referenced by gaussInit().

◆ gvol_

Real const STK::RandBase::gvol_
private

volume of each box and of the remaining tail.

Definition at line 245 of file STK_RandBase.h.

Referenced by gaussInit().

◆ kn

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 250 of file STK_RandBase.h.

Referenced by gaussInit(), randGauss(), and ~RandBase().

◆ wn

Real * STK::RandBase::wn
private

Definition at line 250 of file STK_RandBase.h.

Referenced by gaussInit(), randGauss(), and ~RandBase().


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