mt.c

Go to the documentation of this file.
00001 /* A C-program for MT19937: Integer version (1999/10/28)          */
00002 /*  genrand() generates one pseudorandom unsigned integer (32bit) */
00003 /* which is uniformly distributed among 0 to 2^32-1  for each     */
00004 /* call. sgenrand(seed) sets 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.)                     */
00007 /*   Coded by Takuji Nishimura, considering the suggestions by    */
00008 /* Topher Cooper and Marc Rieffel in July-Aug. 1997.              */
00009 
00010 /* This library is free software under the Artistic license:       */
00011 /* see the file COPYING distributed together with this code.       */
00012 /* For the verification of the code, its output sequence file      */
00013 /* mt19937int.out is attached (2001/4/2)                           */
00014 
00015 /* Copyright (C) 1997, 1999 Makoto Matsumoto and Takuji Nishimura. */
00016 /* Any feedback is very welcome. For any question, comments,       */
00017 /* see http://www.math.keio.ac.jp/matumoto/emt.html or email       */
00018 /* matumoto@math.keio.ac.jp                                        */
00019 
00020 /* REFERENCE                                                       */
00021 /* M. Matsumoto and T. Nishimura,                                  */
00022 /* "Mersenne Twister: A 623-Dimensionally Equidistributed Uniform  */
00023 /* Pseudo-Random Number Generator",                                */
00024 /* ACM Transactions on Modeling and Computer Simulation,           */
00025 /* Vol. 8, No. 1, January 1998, pp 3--30.                          */
00026 
00027 
00028 /*   
00029  *  NOTES
00030  *    slightly modified par Bruno Pincon for inclusion in scilab 
00031  *      - names have changed (for uniformity with the others genators)
00032  *      - add get state routine
00033  *      - add a little verif when the state is changed with the simple
00034  *        procedure
00035  *
00036  *     furthers modifications on May 25 2002 :
00037  *
00038  *     1/ corrections of the followings : 
00039  *        
00040  *        bug 1 : the complete state was returned at the scilab level
00041  *                without the index mti. Now the complete state is a 
00042  *                vector of dim 625 with mti as the first component
00043  *        bug 2 : the set_state doesn't work if the generator was not
00044  *                initialised => add a is_init var and returned
00045  *                the state given with the default initialisation.
00046  *
00047  *     2/ Following the modif in the new version of this generator I have
00048  *       changed the simple initialisation (but not put the init via array)
00049  *
00050  *     Sept 2005 : fix for bug 1568
00051  */
00052 
00053 #include <math.h>
00054 #include "machine.h"
00055 #include "grand.h"            /* to check prototypes */
00056 #include "sciprint.h"
00057 
00058 int set_state_mt_simple(double s);
00059 
00060 
00061 /* Period parameters */  
00062 #define N 624
00063 #define M 397
00064 #define MATRIX_A 0x9908b0df   /* constant vector a */
00065 #define UPPER_MASK 0x80000000 /* most significant w-r bits */
00066 #define LOWER_MASK 0x7fffffff /* least significant r bits */
00067 
00068 /* Tempering parameters */   
00069 #define TEMPERING_MASK_B 0x9d2c5680
00070 #define TEMPERING_MASK_C 0xefc60000
00071 #define TEMPERING_SHIFT_U(y)  (y >> 11)
00072 #define TEMPERING_SHIFT_S(y)  (y << 7)
00073 #define TEMPERING_SHIFT_T(y)  (y << 15)
00074 #define TEMPERING_SHIFT_L(y)  (y >> 18)
00075 
00076 static unsigned long mt[N]; /* the array for the state vector  */
00077 static int mti=N;
00078 static int is_init=0;  
00079 static double DEFAULT_SEED=5489.0;
00080 
00081 extern void sciprint __PARAMS((char *fmt,...));
00082 
00083 unsigned long randmt()
00084 {
00085     unsigned long y;
00086     static unsigned long mag01[2]={0x0, MATRIX_A};
00087     /* mag01[x] = x * MATRIX_A  for x=0,1 */
00088 
00089     if (mti >= N) { /* generate N words at one time */
00090         int kk;
00091 
00092         if ( ! is_init )
00093           set_state_mt_simple(DEFAULT_SEED);
00094 
00095         for (kk=0;kk<N-M;kk++) {
00096             y = (mt[kk]&UPPER_MASK)|(mt[kk+1]&LOWER_MASK);
00097             mt[kk] = mt[kk+M] ^ (y >> 1) ^ mag01[y & 0x1];
00098         }
00099         for (;kk<N-1;kk++) {
00100             y = (mt[kk]&UPPER_MASK)|(mt[kk+1]&LOWER_MASK);
00101             mt[kk] = mt[kk+(M-N)] ^ (y >> 1) ^ mag01[y & 0x1];
00102         }
00103         y = (mt[N-1]&UPPER_MASK)|(mt[0]&LOWER_MASK);
00104         mt[N-1] = mt[M-1] ^ (y >> 1) ^ mag01[y & 0x1];
00105 
00106         mti = 0;
00107     }
00108   
00109     y = mt[mti++];
00110     y ^= TEMPERING_SHIFT_U(y);
00111     y ^= TEMPERING_SHIFT_S(y) & TEMPERING_MASK_B;
00112     y ^= TEMPERING_SHIFT_T(y) & TEMPERING_MASK_C;
00113     y ^= TEMPERING_SHIFT_L(y);
00114 
00115     return ( y );
00116 }
00117 
00118 
00119 int set_state_mt_simple(double s)
00120 {
00121   /*   set the initial state with the simple procedure  */
00122   unsigned long seed;
00123 
00124   if ( s == floor(s) && 0.0 <= s && s <= 4294967295.0)
00125     {
00126       seed = (unsigned long) s;
00127         mt[0]= seed & 0xffffffff;
00128       for (mti=1; mti<N; mti++)
00129         {
00130           mt[mti] =  (1812433253UL * (mt[mti-1] ^ (mt[mti-1] >> 30)) + mti); 
00131           /* See Knuth TAOCP Vol2. 3rd Ed. P.106 for multiplier. */
00132           /* In the previous versions, MSBs of the seed affect   */
00133           /* only MSBs of the array mt[].                        */
00134           /* 2002/01/09 modified by Makoto Matsumoto             */
00135           mt[mti] &= 0xffffffffUL;   /* for >32 bit machines */
00136         }
00137       is_init = 1;
00138       return ( 1 );
00139     }
00140   else
00141     {
00142       sciprint("\n\r bad seed for mt, must be an integer in [0, 2^32-1] \n\r");
00143       return ( 0 );
00144     }
00145 }
00146 
00147 
00148 /* 
00149  *  Initialization by "set_state_simple_mt()" is an example. Theoretically,
00150  *  there are 2^19937-1 possible states as an intial state.           
00151  *   This function allows to choose any of 2^19937-1 ones.            
00152  *   Essential bits in "seed_array[]" is following 19937 bits:        
00153  *      (seed_array[0]&UPPER_MASK), seed_array[1], ..., seed_array[N-1].
00154  *      (seed_array[0]&LOWER_MASK) is discarded.                         
00155  *
00156  *   Theoretically,                                                   
00157  *      (seed_array[0]&UPPER_MASK), seed_array[1], ..., seed_array[N-1] 
00158  *   can take any values except all zeros.                            
00159  */
00160 
00161 int set_state_mt(double seed_array[])
00162 
00163 {
00164     int i, mti_try;
00165 
00166     mti_try = (int) seed_array[0];
00167     if (mti_try < 1  ||  mti_try > 624)
00168       {
00169         sciprint("\n\r the first component of the mt state mt, must be an integer in [1, 624] \n\r");
00170         return ( 0 );
00171       }
00172     is_init = 1;
00173     mti = mti_try;
00174     for (i=0;i<N;i++) 
00175       mt[i] = ((unsigned long) seed_array[i+1]) & 0xffffffff;
00176     return ( 1 );
00177 }
00178 
00179 
00180 /*  To return the state at the scilab level  */
00181 void get_state_mt(double state[])
00182 {
00183     int i;
00184 
00185     if ( ! is_init )
00186       set_state_mt_simple(DEFAULT_SEED);
00187     
00188     state[0] = (double) mti;
00189     for (i=0;i<N;i++) 
00190       state[i+1] = (double) mt[i];
00191 }
00192 
00193 
00194 
00195 
00196 
00197 
00198 
00199 
00200 
00201 

Generated on Sun Mar 4 15:03:59 2007 for Scilab [trunk] by  doxygen 1.5.1