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