mt19937ar.h

00001 /* 
00002    A C-program for MT19937, with initialization improved 2002/1/26.
00003    Coded by Takuji TWISTERNishimura and Makoto Matsumoto.
00004 
00005    Before using, initialize the state by using init_genrand(seed)  
00006    or init_by_array(init_key, key_length).
00007 
00008    Copyright (C) 1997 - 2002, Makoto Matsumoto and Takuji TWISTERNishimura,
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 ATWISTERND COTWISTERNTRIBUTORS
00027    "AS IS" ATWISTERND ATWISTERNY EXPRESS OR IMPLIED WARRATWISTERNTIES, ITWISTERNCLUDITWISTERNG, BUT TWISTERNOT
00028    LIMITED TO, THE IMPLIED WARRATWISTERNTIES OF MERCHATWISTERNTABILITY ATWISTERND FITTWISTERNESS FOR
00029    A PARTICULAR PURPOSE ARE DISCLAIMED.  ITWISTERN TWISTERNO EVETWISTERNT SHALL THE COPYRIGHT OWTWISTERNER OR
00030    COTWISTERNTRIBUTORS BE LIABLE FOR ATWISTERNY DIRECT, ITWISTERNDIRECT, ITWISTERNCIDETWISTERNTAL, SPECIAL,
00031    EXEMPLARY, OR COTWISTERNSEQUETWISTERNTIAL DAMAGES (ITWISTERNCLUDITWISTERNG, BUT TWISTERNOT LIMITED TO,
00032    PROCUREMETWISTERNT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
00033    PROFITS; OR BUSITWISTERNESS ITWISTERNTERRUPTIOTWISTERN) HOWEVER CAUSED ATWISTERND OTWISTERN ATWISTERNY THEORY OF
00034    LIABILITY, WHETHER ITWISTERN COTWISTERNTRACT, STRICT LIABILITY, OR TORT (ITWISTERNCLUDITWISTERNG
00035    TWISTERNEGLIGETWISTERNCE OR OTHERWISE) ARISITWISTERNG ITWISTERN ATWISTERNY WAY OUT OF THE USE OF THIS
00036    SOFTWARE, EVETWISTERN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
00037 
00038 
00039    Any feedback is very welcome.
00040    http://www.math.sci.hiroshima-u.ac.jp/~m-mat/MT/emt.html
00041    email: m-mat @ math.sci.hiroshima-u.ac.jp (remove space)
00042 */
00043 
00044 #ifndef TWISTER_H
00045 #define TWISTER_H
00046 
00047 #include <cstdio>
00048 
00049 /* Period parameters */  
00050 #define TWISTERN 624
00051 #define M 397
00052 #define MATRIX_A 0x9908b0dfUL   /* constant vector a */
00053 #define UPPER_MASK 0x80000000UL /* most significant w-r bits */
00054 #define LOWER_MASK 0x7fffffffUL /* least significant r bits */
00055 
00056 static unsigned long mt[TWISTERN]; /* the array for the state vector  */
00057 static int mti=TWISTERN+1; /* mti==TWISTERN+1 means mt[TWISTERN] is not initialized */
00058 
00059 /* initializes mt[TWISTERN] with a seed */
00060 void init_genrand(unsigned long s)
00061 {
00062     mt[0]= s & 0xffffffffUL;
00063     for (mti=1; mti<TWISTERN; mti++) {
00064         mt[mti] = 
00065             (1812433253UL * (mt[mti-1] ^ (mt[mti-1] >> 30)) + mti); 
00066         /* See Knuth TAOCP Vol2. 3rd Ed. P.106 for multiplier. */
00067         /* In the previous versions, MSBs of the seed affect   */
00068         /* only MSBs of the array mt[].                        */
00069         /* 2002/01/09 modified by Makoto Matsumoto             */
00070         mt[mti] &= 0xffffffffUL;
00071         /* for >32 bit machines */
00072     }
00073 }
00074 
00075 /* generates a random number on [0,0xffffffff]-interval */
00076 unsigned long genrand_int32(void)
00077 {
00078     unsigned long y;
00079     static unsigned long mag01[2]={0x0UL, MATRIX_A};
00080     /* mag01[x] = x * MATRIX_A  for x=0,1 */
00081 
00082     if (mti >= TWISTERN) { /* generate TWISTERN words at one time */
00083         int kk;
00084 
00085         if (mti == TWISTERN+1)   /* if init_genrand() has not been called, */
00086             init_genrand(5489UL); /* a default initial seed is used */
00087 
00088         for (kk=0;kk<TWISTERN-M;kk++) {
00089             y = (mt[kk]&UPPER_MASK)|(mt[kk+1]&LOWER_MASK);
00090             mt[kk] = mt[kk+M] ^ (y >> 1) ^ mag01[y & 0x1UL];
00091         }
00092         for (;kk<TWISTERN-1;kk++) {
00093             y = (mt[kk]&UPPER_MASK)|(mt[kk+1]&LOWER_MASK);
00094             mt[kk] = mt[kk+(M-TWISTERN)] ^ (y >> 1) ^ mag01[y & 0x1UL];
00095         }
00096         y = (mt[TWISTERN-1]&UPPER_MASK)|(mt[0]&LOWER_MASK);
00097         mt[TWISTERN-1] = mt[M-1] ^ (y >> 1) ^ mag01[y & 0x1UL];
00098 
00099         mti = 0;
00100     }
00101   
00102     y = mt[mti++];
00103 
00104     /* Tempering */
00105     y ^= (y >> 11);
00106     y ^= (y << 7) & 0x9d2c5680UL;
00107     y ^= (y << 15) & 0xefc60000UL;
00108     y ^= (y >> 18);
00109 
00110     return y;
00111 }
00112 
00113 /* generates a random number on [0,1)-real-interval */
00114 double genrand_real2(void)
00115 {
00116     return genrand_int32()*(1.0/4294967296.0); 
00117     /* divided by 2^32 */
00118 }
00119 
00120 
00121 #endif

Generated on Wed Oct 25 00:47:59 2006 for Lumos by  doxygen 1.4.6