Generated on Tue Dec 16 12:49:16 2008 for BIU-2.2.0 by doxygen 1.5.1

src/biu/RandomNumberGenerator.cc

Go to the documentation of this file.
00001 #include "biu/RandomNumberGenerator.hh"
00002 #include <stdlib.h>
00003 #include <biu/assertbiu.hh>
00004 
00005 namespace biu {
00006 
00007 
00008     
00009     
00010     RNG_ISO::RNG_ISO(unsigned int seed_) : RandomNumberGenerator(seed_)
00011     {
00012         setSeed(seed_);
00013     }
00014     
00015     RNG_ISO::~RNG_ISO()
00016     {}
00017     
00018     void 
00019     RNG_ISO::setSeed(unsigned int _seed)
00020     {
00021         seed = _seed;
00022         srand(_seed);
00023     }
00024     
00025     unsigned int 
00026     RNG_ISO::getRN()
00027     {
00028         return rand();
00029     }
00030     
00031     unsigned int 
00032     RNG_ISO::getMaxRN() {
00033         return RAND_MAX;
00034     }
00035     
00036     RandomNumberGenerator* 
00037     RNG_ISO::copy(void) {
00038         return new RNG_ISO(*this);
00039     }
00040     
00041     
00042     
00043     
00044     
00045     
00046     
00047     
00048     
00049     
00050     RNG_LCG::RNG_LCG(unsigned int seed_) : RandomNumberGenerator(seed_)
00051     {
00052         setSeed(seed_);
00053     }
00054     
00055     RNG_LCG::~RNG_LCG()
00056     {}
00057     
00058     
00059     void 
00060     RNG_LCG::setSeed(unsigned int _seed)
00061     {
00062         assertbiu(_seed > 0, "seed value has to be > zero!");
00063         seed = _seed;
00064     }
00065     
00066     unsigned int 
00067     RNG_LCG::getRN()
00068     {
00069         
00070         // Wikipedia: using 64 bit integer arithmetics to compute 32 bit integer
00071         // V_{j+1} = (279470273 * V_j) % 4294967291
00072         //  
00073         //        uint64_t z;
00074         //        z = a;
00075         //        z *= 279470273; 
00076         //        z %= 4294967291U;
00077         //        a = z;
00078         //        return a;
00079         
00080         // same algorithm using shift operations
00081         // taken from rand_r.c (glibc 2.5)
00082         unsigned int next = seed;
00083         int result;
00084     
00085         next *= 1103515245;
00086         next += 12345;
00087         result = (unsigned int) (next / 65536) % 2048;
00088     
00089         next *= 1103515245;
00090         next += 12345;
00091         result <<= 10;
00092         result ^= (unsigned int) (next / 65536) % 1024;
00093     
00094         next *= 1103515245;
00095         next += 12345;
00096         result <<= 10;
00097         result ^= (unsigned int) (next / 65536) % 1024;
00098     
00099         seed = next;
00100         
00101         return result;
00102     }
00103 
00104     unsigned int 
00105     RNG_LCG::getMaxRN() {
00106         return 2147483647;
00107     }
00108     
00109     RandomNumberGenerator* 
00110     RNG_LCG::copy(void) {
00111         return new RNG_LCG(*this);
00112     }
00113     
00114 
00115 } // namespace biu