Blender V2.61 - r43446

SCA_RandomNumberGenerator.cpp

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