Jafar
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines
randomIntTmplt.hpp
00001 
00011 #ifndef RANDOM_INT_TEMPLT_HPP
00012 #define RANDOM_INT_TEMPLT_HPP
00013 
00014 #include <boost/random.hpp>
00015 
00029 namespace jafar {
00030   namespace jmath {
00031 
00032     template <class IntType>
00033     class RandomIntTmplt
00034     {
00035     public:
00036       RandomIntTmplt(IntType min_=0, IntType max_=9, uint64_t unSeed=1);
00037       ~RandomIntTmplt(){};
00038       
00044       IntType get();
00045       
00048       void SetSeed(uint64_t unSeed);
00049       
00050     private:
00051       boost::rand48 r_n_gen;
00052       boost::uniform_int<> unif_dist;
00053       boost::variate_generator<boost::rand48&, boost::uniform_int<> > v_g;
00054     };
00055     
00064     template <class IntType>
00065     class RandomIntVectTmplt
00066     {
00067     public:
00068       RandomIntVectTmplt(std::size_t nSize, 
00069        IntType nMin=0, 
00070        IntType nMax=9, 
00071        uint64_t unSeed=1);
00072       ~RandomIntVectTmplt(){};
00073       
00079       std::vector<IntType>& get();
00080       
00087       std::vector<IntType>& getDifferent();
00088       
00091       void SetSeed(uint64_t unSeed);
00092       
00093     private:
00094       boost::rand48 r_n_gen;
00095       boost::uniform_int<> unif_dist;
00096 
00098       boost::variate_generator<boost::rand48&, boost::uniform_int<> > v_g;
00099       
00100       std::vector<IntType> u_;
00101     };
00102     
00103     /* Implementation */
00104     
00105     
00106     
00107     
00108     template <class IntType>
00109     RandomIntTmplt<IntType>::RandomIntTmplt(IntType nMin, 
00110               IntType nMax, 
00111               uint64_t unSeed)
00112       :r_n_gen(unSeed),
00113        unif_dist(nMin, nMax), 
00114        v_g(r_n_gen,unif_dist)
00115     {}
00116     
00117     template <class IntType>
00118     IntType RandomIntTmplt<IntType>::get()
00119     {
00120       return v_g();
00121     }
00122     
00123     template <class IntType>
00124     void  RandomIntTmplt<IntType>::SetSeed(uint64_t unSeed)
00125     {
00126       r_n_gen.seed((uint64_t)unSeed);
00127     }
00128     
00129     
00130     
00131     /*
00132      * class RandomIntVectTmplt
00133      */
00134     
00135     template <class IntType>
00136     RandomIntVectTmplt<IntType>::RandomIntVectTmplt(std::size_t nSize, 
00137                 IntType nMin, 
00138                 IntType nMax, 
00139                 uint64_t unSeed)
00140       : u_(nSize,0), 
00141   r_n_gen(unSeed),
00142   unif_dist(nMin, nMax), 
00143   v_g(r_n_gen,unif_dist)
00144     {}
00145     
00146     template <class IntType>
00147     std::vector<IntType>& RandomIntVectTmplt<IntType>::get()
00148     {
00149       for (unsigned int i=0; i<u_.size(); i++)
00150   u_[i]=v_g();
00151       return u_;
00152     }
00153     
00154     
00155     template <class IntType>
00156     std::vector<IntType>& RandomIntVectTmplt<IntType>::getDifferent()
00157     {
00158       IntType n;
00159       unsigned int i=0;
00160       while (i < u_.size()) {
00161   n = v_g();
00162   // if the number is not already used, add it
00163   if (find(u_.begin(), u_.begin()+i, n)==(u_.begin()+i)) {
00164     u_[i]=n;
00165     i++;
00166   }
00167       }
00168       return u_;
00169     }
00170     
00171 
00172     template <class IntType>
00173     void  RandomIntVectTmplt<IntType>::SetSeed(uint64_t unSeed)
00174     {
00175       r_n_gen.seed((uint64_t)unSeed);
00176     }
00177     
00181     typedef RandomIntTmplt<int> RandomInt;
00182     typedef RandomIntTmplt<unsigned int> RandomUInt;
00183     typedef RandomIntVectTmplt<int> RandomIntVect;
00184     typedef RandomIntVectTmplt<unsigned int> RandomUIntVect;
00185     
00186 
00187   } // namespace jmath
00188 } // namespace jafar
00189 
00190 #endif // RANDOM_INT_TEMPLT_HPP
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines

Generated on Wed Oct 15 2014 00:37:21 for Jafar by doxygen 1.7.6.1
LAAS-CNRS