00001 /* A C-program for MT19937: Real number version */ 00002 /* genrand() generates one pseudorandom real number (double) */ 00003 /* which is uniformly distributed on [0,1]-interval, for each */ 00004 /* call. sgenrand(seed) set initial values to the working area */ 00005 /* of 624 words. Before genrand(), sgenrand(seed) must be */ 00006 /* called once. (seed is any 32-bit integer except for 0). */ 00007 /* Integer generator is obtained by modifying two lines. */ 00008 /* Coded by Takuji Nishimura, considering the suggestions by */ 00009 /* Topher Cooper and Marc Rieffel in July-Aug. 1997. */ 00010 00011 /* This library is free software; you can redistribute it and/or */ 00012 /* modify it under the terms of the GNU Library General Public */ 00013 /* License as published by the Free Software Foundation; either */ 00014 /* version 2 of the License, or (at your option) any later */ 00015 /* version. */ 00016 /* This library is distributed in the hope that it will be useful, */ 00017 /* but WITHOUT ANY WARRANTY; without even the implied warranty of */ 00018 /* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. */ 00019 /* See the GNU Library General Public License for more details. */ 00020 /* You should have received a copy of the GNU Library General */ 00021 /* Public License along with this library; if not, write to the */ 00022 /* Free Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA */ 00023 /* 02111-1307 USA */ 00024 00025 /* Copyright (C) 1997 Makoto Matsumoto and Takuji Nishimura. */ 00026 /* Any feedback is very welcome. For any question, comments, */ 00027 /* see http://www.math.keio.ac.jp/matumoto/emt.html or email */ 00028 /* matumoto@math.keio.ac.jp */ 00029 00030 #include <stdio.h> 00031 00032 /* Period parameters */ 00033 #define N 624 00034 #define M 397 00035 #define MATRIX_A 0x9908b0df /* constant vector a */ 00036 #define UPPER_MASK 0x80000000 /* most significant w-r bits */ 00037 #define LOWER_MASK 0x7fffffff /* least significant r bits */ 00038 00039 /* Tempering parameters */ 00040 #define TEMPERING_MASK_B 0x9d2c5680 00041 #define TEMPERING_MASK_C 0xefc60000 00042 #define TEMPERING_SHIFT_U(y) (y >> 11) 00043 #define TEMPERING_SHIFT_S(y) (y << 7) 00044 #define TEMPERING_SHIFT_T(y) (y << 15) 00045 #define TEMPERING_SHIFT_L(y) (y >> 18) 00046 00047 static unsigned long mt[N]; /* the array for the state vector */ 00048 static int mti=N+1; /* mti==N+1 means mt[N] is not initialized */ 00049 00050 /* initializing the array with a NONZERO seed */ 00051 void 00052 sgenrand(unsigned long seed) 00053 00054 { 00055 /* setting initial seeds to mt[N] using */ 00056 /* the generator Line 25 of Table 1 in */ 00057 /* [KNUTH 1981, The Art of Computer Programming */ 00058 /* Vol. 2 (2nd Ed.), pp102] */ 00059 mt[0]= seed & 0xffffffff; 00060 for (mti=1; mti < N; mti++) 00061 mt[mti] = (69069 * mt[mti-1]) & 0xffffffff; 00062 } 00063 00064 double /* generating reals */ 00065 /* unsigned long */ /* for integer generation */ 00066 genrand() 00067 { 00068 unsigned long y; 00069 static unsigned long mag01[2]={0x0, MATRIX_A}; 00070 /* mag01[x] = x * MATRIX_A for x=0,1 */ 00071 00072 if (mti >= N) { /* generate N words at one time */ 00073 int kk; 00074 00075 if (mti == N+1) /* if sgenrand() has not been called, */ 00076 sgenrand(4357); /* a default initial seed is used */ 00077 00078 for (kk=0;kk < N-M;kk++) { 00079 y = (mt[kk]&UPPER_MASK)|(mt[kk+1]&LOWER_MASK); 00080 mt[kk] = mt[kk+M] ^ (y >> 1) ^ mag01[y & 0x1]; 00081 } 00082 for (;kk < N-1;kk++) { 00083 y = (mt[kk]&UPPER_MASK)|(mt[kk+1]&LOWER_MASK); 00084 mt[kk] = mt[kk+(M-N)] ^ (y >> 1) ^ mag01[y & 0x1]; 00085 } 00086 y = (mt[N-1]&UPPER_MASK)|(mt[0]&LOWER_MASK); 00087 mt[N-1] = mt[M-1] ^ (y >> 1) ^ mag01[y & 0x1]; 00088 00089 mti = 0; 00090 } 00091 00092 y = mt[mti++]; 00093 y ^= TEMPERING_SHIFT_U(y); 00094 y ^= TEMPERING_SHIFT_S(y) & TEMPERING_MASK_B; 00095 y ^= TEMPERING_SHIFT_T(y) & TEMPERING_MASK_C; 00096 y ^= TEMPERING_SHIFT_L(y); 00097 00098 return ( (double)y / (unsigned long)0xffffffff ); /* reals */ 00099 /* return y; */ /* for integer generation */ 00100 } 00101