Random.h

Go to the documentation of this file.
00001 
00006 #ifndef GENETIK_UTIL_RANDOM
00007 #define GENETIK_UTIL_RANDOM
00008 
00009 #include "Log.h"
00010 
00011 namespace genetiK
00012 {
00013 namespace util
00014 {
00015 
00023 class Random
00024 {
00025 protected:
00026 
00032         Random();
00033 
00034         virtual ~Random();
00035 private:
00036         static Random*                  implementation;
00037 
00038 public:
00044         static void                     setImplementation(Random* random);
00045         
00052         static void                     setDefaultImplementation();
00053         
00059         inline static Random*           getImplementation()
00060         {
00061                 if (!implementation)
00062                         Log::getInstance()->warn("Warning: You should call setImplementation before calling Random::getImplentation().");
00063                 return implementation;
00064         }
00069         static void                     deleteImplementation();
00070 
00076         virtual unsigned int            getNextInt()=0;
00077         
00085         virtual unsigned int            getNextInt(unsigned int max)=0;
00086         
00092         virtual double                  getNextReal()=0;
00093 
00094 };
00095 
00096 /*
00097 *  Random Number generator 'Mersenne Twister'                                *
00098 *                                                                            *
00099 *  This random number generator is described in the article by               *
00100 *  M. Matsumoto & T. Nishimura, in:                                          *
00101 *  ACM Transactions on Modeling and Computer Simulation,                     *
00102 *  vol. 8, no. 1, 1998, pp. 3-30.                                            *
00103 *  Details on the initialization scheme can be found at                      *
00104 *  http://www.math.sci.hiroshima-u.ac.jp/~m-mat/MT/emt.html                  *
00105 *                                                                            *
00106 *  Experts consider this an excellent random number generator.               */
00107 
00126 class RandomDefault : public Random{
00127 private:
00128 
00129 #if 0
00130         // constants for MT11213A:
00131         // (32 bit constants cannot be defined as enum in 16-bit compilers)
00132         static const unsigned int MERS_N   351
00133         static const unsigned int MERS_M   175
00134         static const unsigned int MERS_R   19
00135         static const unsigned int MERS_U   11
00136         static const unsigned int MERS_S   7
00137         static const unsigned int MERS_T   15
00138         static const unsigned int MERS_L   17
00139         static const unsigned int MERS_A   0xE4BD75F5
00140         static const unsigned int MERS_B   0x655E5280
00141         static const unsigned int MERS_C   0xFFD58000
00142 #else    
00143         // constants for MT19937:
00144         static const unsigned int MERS_N = 624;
00145         static const unsigned int MERS_M = 397;
00146         static const unsigned int MERS_R = 31;
00147         static const unsigned int MERS_U = 11;
00148         static const unsigned int MERS_S = 7;
00149         static const unsigned int MERS_T = 15;
00150         static const unsigned int MERS_L = 18;
00151         static const unsigned int MERS_A = 0x9908B0DF;
00152         static const unsigned int MERS_B = 0x9D2C5680;
00153         static const unsigned int MERS_C = 0xEFC60000;
00154 #endif
00155 
00156         unsigned int                            stateVector[MERS_N];
00157         unsigned short int                      stateVectorIndex;
00158 
00159         unsigned long int                       seed;
00160 public:
00164         RandomDefault(unsigned int seed);
00165         virtual ~RandomDefault();
00171         virtual unsigned int            getNextInt();
00172         
00180         virtual unsigned int            getNextInt(unsigned int max);
00181         
00187         virtual double                  getNextReal();
00188 };
00189 
00190 }
00191 }
00192 
00193 #endif

Generated on Thu Feb 23 12:20:45 2006 for GenetiK by  doxygen 1.4.6