Main Page | Class Hierarchy | Alphabetical List | Class List | File List | Class Members | File Members

MTRandom.h

Go to the documentation of this file.
00001 // Mersenne Twister random number generator
00002 // It is proved that the period is 2^19937-1, and 623-dimensional equidistribution property is assured.
00003 // http://www.math.sci.hiroshima-u.ac.jp/~m-mat/MT/emt.html
00004 // About 1.3 times slower than rand() on Windows XP / VC++6
00005 
00006 // C++ include file for MT19937, with initialization improved 2002/1/26.
00007 // Coded by Takuji Nishimura and Makoto Matsumoto.
00008 //
00009 // The generators returning floating point numbers are based on
00010 // a version by Isaku Wada, 2002/01/09
00011 //
00012 // Ported to C++ by Jasper Bedaux 2003/1/1 (see http://www.bedaux.net/mtrand/).
00013 //
00014 // Modified by Razvan Florian (http://coneural.org/florian/) to implement static 
00015 // methods for random number generation, 2005/07/27
00016 //
00017 // Copyright (C) 1997 - 2002, Makoto Matsumoto and Takuji Nishimura
00018 // Copyright (C) 2005, Razvan Florian
00019 // All rights reserved.
00020 //
00021 // Redistribution and use in source and binary forms, with or without
00022 // modification, are permitted provided that the following conditions
00023 // are met:
00024 //
00025 // 1. Redistributions of source code must retain the above copyright
00026 //    notice, this list of conditions and the following disclaimer.
00027 //
00028 // 2. Redistributions in binary form must reproduce the above copyright
00029 //    notice, this list of conditions and the following disclaimer in the
00030 //    documentation and/or other materials provided with the distribution.
00031 //
00032 // 3. The names of its contributors may not be used to endorse or promote
00033 //    products derived from this software without specific prior written
00034 //    permission.
00035 //
00036 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
00037 // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
00038 // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
00039 // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
00040 // CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
00041 // EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
00042 // PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
00043 // PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
00044 // LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
00045 // NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
00046 // SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
00047 //
00048 // Any feedback is very welcome.
00049 // email: matumoto@math.keio.ac.jp
00050 
00051 
00052 #if !defined(AFX_MTRANDOM_H__80B8318A_FECC_423A_9199_FF3E5F3A05D3__INCLUDED_)
00053 #define AFX_MTRANDOM_H__80B8318A_FECC_423A_9199_FF3E5F3A05D3__INCLUDED_
00054 
00055 // the following constants are not class members because Visual C++ 6 does not allow it
00056 static const int MTRandomN = 624, MTRandomM = 397; // compile time constants
00057 
00058 class MTRandom  {
00059 public:
00060    MTRandom();
00061    virtual ~MTRandom();
00062 
00063    static void seed(unsigned long); // seed with 32 bit integer
00064    static void seed(const unsigned long*, int size); // seed with array
00065 
00067    inline static unsigned long int getLongInt() { 
00068       if (p == MTRandomN) gen_state(); // new state vector needed
00069       // gen_state() is split off to be non-inline, because it is only called once
00070       // in every 624 calls and otherwise irand() would become too big to get inlined
00071       unsigned long x = state[p++];
00072       x ^= (x >> 11);
00073       x ^= (x << 7) & 0x9D2C5680UL;
00074       x ^= (x << 15) & 0xEFC60000UL;
00075       return x ^ (x >> 18);
00076    }
00077 
00079    inline static unsigned int getInt(unsigned int n){
00080       return (unsigned int)(getLongInt() % (unsigned long int)(n+1));
00081    }
00082 
00084    inline static float getFloat(){
00085       return static_cast<float>(getLongInt()) * (1.f / 4294967296.f);  // divided by 2^32
00086    }
00087 
00089    inline static double getDouble(){
00090       return static_cast<double>(getLongInt()) * (1. / 4294967296.);  // divided by 2^32
00091    }
00092 
00094    inline static double getDoubleClosed(){
00095       return static_cast<double>(getLongInt()) * (1. / 4294967295.);  // divided by 2^32 - 1
00096    }
00097 
00099    inline static double getDoubleOpen(){
00100       return (static_cast<double>(getLongInt()) + .5) * (1. / 4294967296.);  // divided by 2^32
00101    }
00102 
00104    inline static double getDoubleHR(){
00105       return (static_cast<double>(getLongInt() >> 5) * 67108864. + 
00106       static_cast<double>(getLongInt() >> 6)) * (1. / 9007199254740992.);
00107    }
00108 
00109 private:
00110 
00111    // the variables below are static (no duplicates can exist)
00112    static unsigned long int state[MTRandomN]; // state vector array
00113    static int p; // position in state array
00114 
00115    static void gen_state(); // generate new state
00116 
00117    inline static unsigned long twiddle(unsigned long u, unsigned long v) { //used by gen_state()
00118       return (((u & 0x80000000UL) | (v & 0x7FFFFFFFUL)) >> 1)
00119          ^ ((v & 1UL) ? 0x9908B0DFUL : 0x0UL);
00120    }
00121   
00122 
00123    // make copy constructor and assignment operator unavailable, they don't make sense
00124   MTRandom(const MTRandom&); // copy constructor not defined
00125   void operator=(const MTRandom&); // assignment operator not defined
00126 
00128    //void saveState();
00130    //void verifyState(unsigned long s);
00131 };
00132 
00133 #endif // !defined(AFX_MTRANDOM_H__80B8318A_FECC_423A_9199_FF3E5F3A05D3__INCLUDED_)

Thyrix homepageUsers' guide

(C) Arxia 2004-2005