| TerraLib 4.1

- Main Page
- Related Pages
- Modules
- Namespaces
- Classes
- Files
- Examples

- File List
- File Members

E:/Projetos_Primeiro_Semestre_2012/TerraView/terralib/src/terralib/utils/mtrand.h
Go to the documentation of this file.

    00001 // mtrand.h
    00002 // C++ include file for MT19937, with initialization improved 2002/1/26.
    00003 // Coded by Takuji Nishimura and Makoto Matsumoto.
    00004 // Ported to C++ by Jasper Bedaux 2003/1/1 (see http://www.bedaux.net/mtrand/).
    00005 // The generators returning floating point numbers are based on
    00006 // a version by Isaku Wada, 2002/01/09
    00007 //
    00008 // Copyright (C) 1997 - 2002, Makoto Matsumoto and Takuji Nishimura,
    00009 // All rights reserved.
    00010 //
    00011 // Redistribution and use in source and binary forms, with or without
    00012 // modification, are permitted provided that the following conditions
    00013 // are met:
    00014 //
    00015 // 1. Redistributions of source code must retain the above copyright
    00016 //    notice, this list of conditions and the following disclaimer.
    00017 //
    00018 // 2. Redistributions in binary form must reproduce the above copyright
    00019 //    notice, this list of conditions and the following disclaimer in the
    00020 //    documentation and/or other materials provided with the distribution.
    00021 //
    00022 // 3. The names of its contributors may not be used to endorse or promote
    00023 //    products derived from this software without specific prior written
    00024 //    permission.
    00025 //
    00026 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
    00027 // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
    00028 // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
    00029 // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
    00030 // CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
    00031 // EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
    00032 // PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
    00033 // PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
    00034 // LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
    00035 // NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
    00036 // SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
    00037 //
    00038 // Any feedback is very welcome.
    00039 // http://www.math.keio.ac.jp/matumoto/emt.html
    00040 // email: matumoto@math.keio.ac.jp
    00041 //
    00042 // Feedback about the C++ port should be sent to Jasper Bedaux,
    00043 // see http://www.bedaux.net/mtrand/ for e-mail address and info.
    00044
    00045 #ifndef MTRAND_H
    00046 #define MTRAND_H
    00047
    00048 #include "TeUtilsDefines.h"
    00049
    00050 class TLUTILS_DLL MTRand_int32 { // Mersenne Twister random number generator
    00051 public:
    00052 // default constructor: uses default seed only if this is the first instance
    00053   MTRand_int32() { if (!init) seed(5489UL); init = true; }
    00054 // constructor with 32 bit int as seed
    00055   MTRand_int32(unsigned long s) { seed(s); init = true; }
    00056 // constructor with array of size 32 bit ints as seed
    00057   MTRand_int32(const unsigned long* array, int size) { seed(array, size); init = true; }
    00058 // the two seed functions
    00059   void seed(unsigned long); // seed with 32 bit integer
    00060   void seed(const unsigned long*, int size); // seed with array
    00061 // overload operator() to make this a generator (functor)
    00062   unsigned long operator()() { return rand_int32(); }
    00063 // 2007-02-11: made the destructor virtual; thanks "double more" for pointing this out
    00064   virtual ~MTRand_int32() {} // destructor
    00065 protected: // used by derived classes, otherwise not accessible; use the ()-operator
    00066   unsigned long rand_int32(); // generate 32 bit random integer
    00067 private:
    00068   static const int n = 624; // compile time constants
    00069   static const int m = 397; // compile time constants
    00070 // the variables below are static (no duplicates can exist)
    00071   static unsigned long state[n]; // state vector array
    00072   static int p; // position in state array
    00073   static bool init; // true if init function is called
    00074 // private functions used to generate the pseudo random numbers
    00075   unsigned long twiddle(unsigned long, unsigned long); // used by gen_state()
    00076   void gen_state(); // generate new state
    00077 // make copy constructor and assignment operator unavailable, they don't make sense
    00078   MTRand_int32(const MTRand_int32&); // copy constructor not defined
    00079   void operator=(const MTRand_int32&); // assignment operator not defined
    00080 };
    00081
    00082 // inline for speed, must therefore reside in header file
    00083 inline unsigned long MTRand_int32::twiddle(unsigned long u, unsigned long v) {
    00084   return (((u & 0x80000000UL) | (v & 0x7FFFFFFFUL)) >> 1)
    00085     ^ ((v & 1UL) ? 0x9908B0DFUL : 0x0UL);
    00086 }
    00087
    00088 inline unsigned long MTRand_int32::rand_int32() { // generate 32 bit random int
    00089   if (p == n) gen_state(); // new state vector needed
    00090 // gen_state() is split off to be non-inline, because it is only called once
    00091 // in every 624 calls and otherwise irand() would become too big to get inlined
    00092   unsigned long x = state[p++];
    00093   x ^= (x >> 11);
    00094   x ^= (x << 7) & 0x9D2C5680UL;
    00095   x ^= (x << 15) & 0xEFC60000UL;
    00096   return x ^ (x >> 18);
    00097 }
    00098
    00099 // generates double floating point numbers in the half-open interval [0, 1)
    00100 class TLUTILS_DLL MTRand : public MTRand_int32 {
    00101 public:
    00102   MTRand() : MTRand_int32() {}
    00103   MTRand(unsigned long seed) : MTRand_int32(seed) {}
    00104   MTRand(const unsigned long* seed, int size) : MTRand_int32(seed, size) {}
    00105   ~MTRand() {}
    00106   double operator()() {
    00107     return static_cast<double>(rand_int32()) * (1. / 4294967296.); } // divided by 2^32
    00108 private:
    00109   MTRand(const MTRand&); // copy constructor not defined
    00110   void operator=(const MTRand&); // assignment operator not defined
    00111 };
    00112
    00113 // generates double floating point numbers in the closed interval [0, 1]
    00114 class TLUTILS_DLL MTRand_closed : public MTRand_int32 {
    00115 public:
    00116   MTRand_closed() : MTRand_int32() {}
    00117   MTRand_closed(unsigned long seed) : MTRand_int32(seed) {}
    00118   MTRand_closed(const unsigned long* seed, int size) : MTRand_int32(seed, size) {}
    00119   ~MTRand_closed() {}
    00120   double operator()() {
    00121     return static_cast<double>(rand_int32()) * (1. / 4294967295.); } // divided by 2^32 - 1
    00122 private:
    00123   MTRand_closed(const MTRand_closed&); // copy constructor not defined
    00124   void operator=(const MTRand_closed&); // assignment operator not defined
    00125 };
    00126
    00127 // generates double floating point numbers in the open interval (0, 1)
    00128 class TLUTILS_DLL MTRand_open : public MTRand_int32 {
    00129 public:
    00130   MTRand_open() : MTRand_int32() {}
    00131   MTRand_open(unsigned long seed) : MTRand_int32(seed) {}
    00132   MTRand_open(const unsigned long* seed, int size) : MTRand_int32(seed, size) {}
    00133   ~MTRand_open() {}
    00134   double operator()() {
    00135     return (static_cast<double>(rand_int32()) + .5) * (1. / 4294967296.); } // divided by 2^32
    00136 private:
    00137   MTRand_open(const MTRand_open&); // copy constructor not defined
    00138   void operator=(const MTRand_open&); // assignment operator not defined
    00139 };
    00140
    00141 // generates 53 bit resolution doubles in the half-open interval [0, 1)
    00142 class TLUTILS_DLL MTRand53 : public MTRand_int32 {
    00143 public:
    00144   MTRand53() : MTRand_int32() {}
    00145   MTRand53(unsigned long seed) : MTRand_int32(seed) {}
    00146   MTRand53(const unsigned long* seed, int size) : MTRand_int32(seed, size) {}
    00147   ~MTRand53() {}
    00148   double operator()() {
    00149     return (static_cast<double>(rand_int32() >> 5) * 67108864. +
    00150       static_cast<double>(rand_int32() >> 6)) * (1. / 9007199254740992.); }
    00151 private:
    00152   MTRand53(const MTRand53&); // copy constructor not defined
    00153   void operator=(const MTRand53&); // assignment operator not defined
    00154 };
    00155
    00156 #endif // MTRAND_H

All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines