Blender V2.61 - r43446

MT_random.cpp

Go to the documentation of this file.
00001 /*
00002  * ***** BEGIN GPL LICENSE BLOCK *****
00003  *
00004  * This program is free software; you can redistribute it and/or
00005  * modify it under the terms of the GNU General Public License
00006  * as published by the Free Software Foundation; either version 2
00007  * of the License, or (at your option) any later version.
00008  *
00009  * This program is distributed in the hope that it will be useful,
00010  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00011  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00012  * GNU General Public License for more details.
00013  *
00014  * You should have received a copy of the GNU General Public License
00015  * along with this program; if not, write to the Free Software Foundation,
00016  * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
00017  *
00018  * The Original Code is Copyright (C) 2001-2002 by NaN Holding BV.
00019  * All rights reserved.
00020  *
00021  * The Original Code is: all of this file.
00022  *
00023  * Contributor(s): none yet.
00024  *
00025  * ***** END GPL LICENSE BLOCK *****
00026  */
00027 
00033 /* A C-program for MT19937: Real number version                */
00034 
00035 /*   genrand() generates one pseudorandom real number (double) */
00036 /* which is uniformly distributed on [0,1]-interval, for each  */
00037 /* call. sgenrand(seed) set initial values to the working area */
00038 /* of 624 words. Before genrand(), sgenrand(seed) must be      */
00039 /* called once. (seed is any 32-bit integer except for 0).     */
00040 /* Integer generator is obtained by modifying two lines.       */
00041 /*   Coded by Takuji Nishimura, considering the suggestions by */
00042 /* Topher Cooper and Marc Rieffel in July-Aug. 1997.           */
00043 
00044 /* This library is free software; you can redistribute it and/or   */
00045 /* modify it under the terms of the GNU Library General Public     */
00046 /* License as published by the Free Software Foundation; either    */
00047 /* version 2 of the License, or (at your option) any later         */
00048 /* version.                                                        */
00049 /* This library is distributed in the hope that it will be useful, */
00050 /* but WITHOUT ANY WARRANTY; without even the implied warranty of  */
00051 /* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.            */
00052 /* See the GNU Library General Public License for more details.    */
00053 /* You should have received a copy of the GNU Library General      */
00054 /* Public License along with this library; if not, write to the    */
00055 /* Free Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA   */ 
00056 /* 02110-1301, USA                                                 */
00057 
00058 /* Copyright (C) 1997 Makoto Matsumoto and Takuji Nishimura.       */
00059 /* When you use this, send an email to: matumoto@math.keio.ac.jp   */
00060 /* with an appropriate reference to your work.                     */
00061 
00062 #include "MT_random.h"
00063 
00064 /* Period parameters */  
00065 #define N 624
00066 #define M 397
00067 #define MATRIX_A 0x9908b0df   /* constant vector a */
00068 #define UPPER_MASK 0x80000000 /* most significant w-r bits */
00069 #define LOWER_MASK 0x7fffffff /* least significant r bits */
00070 
00071 /* Tempering parameters */   
00072 #define TEMPERING_MASK_B 0x9d2c5680
00073 #define TEMPERING_MASK_C 0xefc60000
00074 #define TEMPERING_SHIFT_U(y)  (y >> 11)
00075 #define TEMPERING_SHIFT_S(y)  (y << 7)
00076 #define TEMPERING_SHIFT_T(y)  (y << 15)
00077 #define TEMPERING_SHIFT_L(y)  (y >> 18)
00078 
00079 static unsigned int mt[N]; /* the array for the state vector  */
00080 static int mti = N+1; /* mti==N+1 means mt[N] is not initialized */
00081 
00082 /* initializing the array with a NONZERO seed */
00083 void MT_srand(unsigned int seed)
00084 {
00085     /* setting initial seeds to mt[N] using         */
00086     /* the generator Line 25 of Table 1 in          */
00087     /* [KNUTH 1981, The Art of Computer Programming */
00088     /*    Vol. 2 (2nd Ed.), pp102]                  */
00089     mt[0] = seed & 0xffffffff;
00090     for (mti = 1; mti < N; mti++)
00091         mt[mti] = (69069 * mt[mti-1]) & 0xffffffff;
00092 }
00093 
00094 unsigned int MT_rand()
00095 {
00096     static unsigned int mag01[2] = { 0x0, MATRIX_A };
00097     /* mag01[x] = x * MATRIX_A  for x=0,1 */
00098 
00099     unsigned int y;
00100 
00101     if (mti >= N) { /* generate N words at one time */
00102         int kk;
00103         
00104         if (mti == N+1)   /* if sgenrand() has not been called, */
00105             MT_srand(4357); /* a default initial seed is used   */
00106         
00107         for (kk = 0; kk < N - M; kk++) {
00108             y = (mt[kk] & UPPER_MASK) | (mt[kk+1] & LOWER_MASK);
00109             mt[kk] = mt[kk+M] ^ (y >> 1) ^ mag01[y & 0x1];
00110         }
00111         for (; kk < N-1; kk++) {
00112             y = (mt[kk] & UPPER_MASK) | (mt[kk+1] & LOWER_MASK);
00113             mt[kk] = mt[kk+(M-N)] ^ (y >> 1) ^ mag01[y & 0x1];
00114         }
00115         y = (mt[N-1] & UPPER_MASK) | (mt[0] & LOWER_MASK);
00116         mt[N-1] = mt[M-1] ^ (y >> 1) ^ mag01[y & 0x1];
00117 
00118         mti = 0;
00119     }
00120   
00121     y = mt[mti++];
00122     y ^= TEMPERING_SHIFT_U(y);
00123     y ^= TEMPERING_SHIFT_S(y) & TEMPERING_MASK_B;
00124     y ^= TEMPERING_SHIFT_T(y) & TEMPERING_MASK_C;
00125     y ^= TEMPERING_SHIFT_L(y);
00126 
00127     return y;
00128 }
00129 
00130 #undef N 
00131 #undef M
00132 #undef MATRIX_A
00133 #undef UPPER_MASK 
00134 #undef LOWER_MASK 
00135 
00136 /* Tempering parameters */   
00137 #undef TEMPERING_MASK_B 
00138 #undef TEMPERING_MASK_C 
00139 #undef TEMPERING_SHIFT_U  
00140 #undef TEMPERING_SHIFT_S  
00141 #undef TEMPERING_SHIFT_T  
00142 #undef TEMPERING_SHIFT_L  
00143