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