Shark Machine Learning Library
  • About Shark
  • Sourceforge
    • Project Summary
    • Downloads
    • Subversion Repository
  • Getting Started
  • Tutorials
  • FAQ
  • Main Modules
    • ReClaM
    • EALib
    • MOO-EALib
    • Fuzzy
  • Tools
    • Mixture
    • Array
    • Rng
    • LinAlg
    • FileUtil
  • Main Page
  • Classes

RNG.h

Go to the documentation of this file.
00001 //===========================================================================
00044 //===========================================================================
00045 
00046 #ifndef __RNG_H
00047 #define __RNG_H
00048 
00049 #include <cstdlib>
00050 #include <sys/types.h>
00051 #include <ctime>
00052 
00053 //===========================================================================
00130 class RNG
00131 {
00132 public:
00133 
00134     virtual ~RNG()
00135     {};
00136 
00137     //========================================================================
00160     RNG(long s = 1)
00161     {
00162         seed(s);
00163     }
00164 
00165 
00166     //========================================================================
00191     void       seed(long s = time(NULL))
00192     {
00193         //if( s == 0 ) s = time( NULL );
00194         initialSx = sx = (s         & 0xff) + 1;
00195         initialSy = sy = ((s >>  8) & 0xff) + 10000;
00196         initialSz = sz = ((s >> 16) & 0xffff) + 3000;
00197     }
00198 
00199 
00200     //========================================================================
00222     void       reset()
00223     {
00224         sx = initialSx;
00225         sy = initialSy;
00226         sz = initialSz;
00227     }
00228 
00229 
00230     //========================================================================
00253     long       genLong()
00254     {
00255         const long MaxRand = 0x7fffffffL;
00256 
00257         return (long)(genDouble() *((double)MaxRand + 1));
00258     }
00259 
00260 
00261     //========================================================================
00282     double     genDouble()
00283     {
00284         double rn;
00285 
00286         // the three congruential generators
00287         sx = (unsigned)(sx * 171UL % 30269UL);
00288         sy = (unsigned)(sy * 172UL % 30307UL);
00289         sz = (unsigned)(sz * 170UL % 30323UL);
00290 
00291         rn = sx / 30269. + sy / 30307. + sz / 30323.;
00292 
00293         return rn - (unsigned)rn;
00294     }
00295 
00296 
00297     //========================================================================
00321     void       getStatus(unsigned &x, unsigned &y, unsigned &z) const
00322     {
00323         x = sx;
00324         y = sy;
00325         z = sz;
00326     }
00327 
00328 
00329     //========================================================================
00354     void       setStatus(const unsigned x, const unsigned y, const unsigned z)
00355     {
00356         // initial state is not restored
00357         sx = x;
00358         sy = y;
00359         sz = z;
00360     }
00361 
00362     //========================================================================
00383     virtual double operator()()
00384     {
00385         return genDouble();
00386     }
00387 
00388 
00391     static RNG globalRng;
00392 
00393 
00394 private:
00395 
00396     // Seed value for internal generator no. 1.
00397     unsigned initialSx;
00398 
00399     // Seed value for internal generator no. 2.
00400     unsigned initialSy;
00401 
00402     // Seed value for internal generator no. 3.
00403     unsigned initialSz;
00404 
00405     // Current value of ni,1 (see description of class).
00406     unsigned sx,
00407 
00408     // Current value of ni,2 (see description of class).
00409     sy,
00410 
00411     // Current value of ni,3 (see description of class).
00412     sz;
00413 };
00414 
00415 #endif  /* !__RNG_H */
00416 
00417 
00418 
00419 
00420 
00421