G:/ScriptBasic/source/tools/mt19937.c

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

Generated on Sun Mar 12 23:56:32 2006 for ScriptBasic by  doxygen 1.4.6-NO