random_number_generator.hpp
Go to the documentation of this file.
00001 
00008 /*****************************************************************************
00009 ** Ifdefs
00010 *****************************************************************************/
00011 
00012 #ifndef ECL_TIME_RANDOM_NUMBER_GENERATOR_HPP_
00013 #define ECL_TIME_RANDOM_NUMBER_GENERATOR_HPP_
00014 
00015 /*****************************************************************************
00016 ** Includes
00017 *****************************************************************************/
00018 
00019 #include <cmath>
00020 #include <ctime>
00021 #include <cstdlib>
00022 #include <ecl/config.hpp>
00023 
00024 #if defined(ECL_IS_WIN32)
00025   getpid = _getpid
00026 #endif
00027 
00028 /*****************************************************************************
00029 ** Namespaces
00030 *****************************************************************************/
00031 
00032 namespace ecl {
00033 
00034 /*****************************************************************************
00035 ** Random Number Generator
00036 *****************************************************************************/
00040 template<typename T>
00041 class RandomNumberGenerator
00042 {
00043 public:
00044   RandomNumberGenerator( const unsigned int randSeed )
00045   {
00046     srand( randSeed );
00047   }
00048 
00049   RandomNumberGenerator()
00050   {
00051     RandomNumberGenerator::seed();
00052   }
00053 
00054   T uniform( const T & range)
00055   {
00056     return (uniform()-0.5)*2.0*range;
00057   }
00058 
00059   T uniform()
00060   {
00061     return static_cast<T>(rand())/static_cast<T>(RAND_MAX);
00062   }
00063 
00064   T gaussian( const T & std, const T & mu = 0 )
00065   {
00066     T x1, x2, w, y1;
00067     static T y2;
00068     static bool use_last = false;
00069 
00070     if (use_last)  /* use value from previous call */
00071     {
00072       y1 = y2;
00073       use_last = false;
00074     }
00075     else
00076     {
00077       do {
00078           x1 = 2.0 * uniform() - 1.0;
00079           x2 = 2.0 * uniform() - 1.0;
00080           w = x1 * x1 + x2 * x2;
00081       } while ( w >= 1.0 );
00082 
00083       w = std::sqrt( (-2.0 * std::log( w ) ) / w );
00084       y1 = x1 * w;
00085       y2 = x2 * w;
00086       use_last = true;
00087     }
00088 
00089     return mu + y1 * std;
00090   }
00091 
00092 private:
00111   static void seed()
00112   {
00113     unsigned long a = clock();
00114     unsigned long b = ::time(NULL);
00115     unsigned long c = getpid();
00116     a=a-b;  a=a-c;  a=a^(c >> 13);
00117     b=b-c;  b=b-a;  b=b^(a << 8);
00118     c=c-a;  c=c-b;  c=c^(b >> 13);
00119     a=a-b;  a=a-c;  a=a^(c >> 12);
00120     b=b-c;  b=b-a;  b=b^(a << 16);
00121     c=c-a;  c=c-b;  c=c^(b >> 5);
00122     a=a-b;  a=a-c;  a=a^(c >> 3);
00123     b=b-c;  b=b-a;  b=b^(a << 10);
00124     c=c-a;  c=c-b;  c=c^(b >> 15);
00125     srand(c);
00126   }
00127 };
00128 
00129 } // namespace ecl
00130 
00131 
00132 #endif /* ECL_TIME_RANDOM_NUMBER_GENERATOR_HPP_ */


ecl_time
Author(s): Daniel Stonier
autogenerated on Thu Jun 6 2019 21:17:28