STK++ 1.0
STK_RandBase.h
Go to the documentation of this file.
00001 /*--------------------------------------------------------------------*/
00002 /*     Copyright (C) 2004-2007  Serge Iovleff
00003 
00004     This program is free software; you can redistribute it and/or modify
00005     it under the terms of the GNU Lesser General Public License as
00006     published by the Free Software Foundation; either version 2 of the
00007     License, or (at your option) any later version.
00008 
00009     This program is distributed in the hope that it will be useful,
00010     but WITHOUT ANY WARRANTY; without even the implied warranty of
00011     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00012     GNU Lesser General Public License for more details.
00013 
00014     You should have received a copy of the GNU Lesser General Public
00015     License along with this program; if not, write to the
00016     Free Software Foundation, Inc.,
00017     59 Temple Place,
00018     Suite 330,
00019     Boston, MA 02111-1307
00020     USA
00021 
00022     Contact : Serge.Iovleff@stkpp.org
00023 */
00024 
00025 /*
00026  * Project:  Laws
00027  * Purpose:  Main pseudo-random uniform, gaussian and exponentiel
00028  *           generators.
00029  * Author:   Serge Iovleff, serge.iovleff@stkpp.org
00030  **/
00031 
00039 #ifndef STK_RANDBASE_H
00040 #define STK_RANDBASE_H
00041 
00042 #include "../../STKernel/include/STK_Misc.h"
00043 #include "../../STKernel/include/STK_Integer.h"
00044 #include "../../STKernel/include/STK_Real.h"
00045 
00046 #include "../../Sdk/include/STK_ITContainer1D.h"
00047 
00048 // MersenneTwister header.
00049 #include "MersenneTwister.h"
00050 
00051 namespace STK
00052 {
00076 class RandBase : protected MTRand
00077 {
00078   private:
00079 
00080     /*  Gauss parameters.                                             */
00084     const Integer gsize_;
00085 
00087     const Real    glimit_;
00088 
00090     const Real    gvol_;
00091 
00096     Real *kn, *wn, *fn;
00097 
00098   public:
00106     RandBase( const Real    &glimit = 3.442619855899
00107             , const Real    &gvol   = 9.91256303526217e-3
00108             , Integer const& gsize  = 128
00109             );
00110 
00117     RandBase( Integer const& oneSeed
00118             , const Real    &glimit = 3.442619855899
00119             , const Real    &gvol   = 9.91256303526217e-3
00120             , Integer const& gsize  = 128
00121             );
00122 
00129     template< class TContainer1D>
00130     RandBase( const ITContainer1D<Integer, TContainer1D> &bigSeed
00131             , const Real    &glimit = 3.442619855899
00132             , const Real    &gvol   = 9.91256303526217e-3
00133             , Integer const& gsize  = 128
00134             )
00135             : gsize_(gsize)
00136             , glimit_(glimit)
00137             , gvol_(gvol)
00138     {
00139       // dimension
00140       Integer first = bigSeed.first(), last = bigSeed.last()
00141             , size = bigSeed.size();
00142       uint32 arraySeed[size];
00143       // cast Integer in uint32
00144       for (Integer i=first; i<=last; i++)
00145       {
00146         arraySeed[i-first] = uint32(bigSeed[i]);
00147       }
00148       // Re-seeding functions with same behavior as initializers
00149       seed(arraySeed, size);
00150       // initialize zigourat method for gaussian random generator
00151       gaussInit();
00152     }
00153 
00155     ~RandBase();
00156 
00165     inline Integer randDiscreteUnif()
00166     { return Integer(randInt());}
00167 
00176     inline Real randUnif()
00177     { return Real(randDblExc());}
00178 
00180    inline Real operator()()
00181    { return randUnif(); }
00182 
00191     Real randGauss(Real const& mu = 0, Real const& sigma = 1);
00192 
00200     Real randExp();
00201 
00206     template< class TContainer1D>
00207     inline void randDiscreteUnif(ITContainer1D<Integer, TContainer1D>& A)
00208     { for (Integer i=A.first(); i<=A.last(); i++)
00209         A[i] = randDiscreteUnif();
00210     }
00211 
00216     template< class TContainer1D>
00217     inline void randUnif(ITContainer1D<Real, TContainer1D>& A)
00218     { for (Integer i=A.first(); i<=A.last(); i++)
00219         A[i] = randUnif();
00220     }
00221 
00226     template< class TContainer1D>
00227     inline void randGauss(ITContainer1D<Real , TContainer1D>& A)
00228     { for (Integer i=A.first(); i<=A.last(); i++)
00229         A[i] = randGauss();
00230     }
00231 
00236     template< class TContainer1D>
00237     inline void randExp(ITContainer1D<Real , TContainer1D>& A)
00238     { for (Integer i=A.first(); i<=A.last(); i++)
00239         A[i] = randExp();
00240     }
00241 
00242   private:
00244     void gaussInit();
00245 };
00246 
00247 } // namespace STK
00248 
00249 #endif //STK_RANDBASE_H
00250