Main Page   Namespace List   Class Hierarchy   Compound List   File List   Compound Members   File Members  

SCA_RandomNumberGenerator.cpp

Go to the documentation of this file.
00001 
00008 
00009 /* A C-program for MT19937: Real number version                */
00010 /*   genrand() generates one pseudorandom real number (double) */
00011 /* which is uniformly distributed on [0,1]-interval, for each  */
00012 /* call. sgenrand(seed) set initial values to the working area */
00013 /* of 624 words. Before genrand(), sgenrand(seed) must be      */
00014 /* called once. (seed is any 32-bit integer except for 0).     */
00015 /* Integer generator is obtained by modifying two lines.       */
00016 /*   Coded by Takuji Nishimura, considering the suggestions by */
00017 /* Topher Cooper and Marc Rieffel in July-Aug. 1997.           */
00018 
00019 /* This library is free software; you can redistribute it and/or   */
00020 /* modify it under the terms of the GNU Library General Public     */
00021 /* License as published by the Free Software Foundation; either    */
00022 /* version 2 of the License, or (at your option) any later         */
00023 /* version.                                                        */
00024 /* This library is distributed in the hope that it will be useful, */
00025 /* but WITHOUT ANY WARRANTY; without even the implied warranty of  */
00026 /* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.            */
00027 /* See the GNU Library General Public License for more details.    */
00028 /* You should have received a copy of the GNU Library General      */
00029 /* Public License along with this library; if not, write to the    */
00030 /* Free Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA   */ 
00031 /* 02111-1307  USA                                                 */
00032 
00033 /* Copyright (C) 1997 Makoto Matsumoto and Takuji Nishimura.       */
00034 /* When you use this, send an email to: [email protected]   */
00035 /* with an appropriate reference to your work.                     */
00036 
00037 #include <limits.h>
00038 #include "SCA_RandomNumberGenerator.h"
00039 
00040 /* Period parameters */  
00041 #define N 624
00042 #define M 397
00043 #define MATRIX_A 0x9908b0df   /* constant vector a */
00044 #define UPPER_MASK 0x80000000 /* most significant w-r bits */
00045 #define LOWER_MASK 0x7fffffff /* least significant r bits */
00046 
00047 /* Tempering parameters */   
00048 #define TEMPERING_MASK_B 0x9d2c5680
00049 #define TEMPERING_MASK_C 0xefc60000
00050 #define TEMPERING_SHIFT_U(y)  (y >> 11)
00051 #define TEMPERING_SHIFT_S(y)  (y << 7)
00052 #define TEMPERING_SHIFT_T(y)  (y << 15)
00053 #define TEMPERING_SHIFT_L(y)  (y >> 18)
00054 
00055 SCA_RandomNumberGenerator::SCA_RandomNumberGenerator(long seed) {
00056         int mti = N + 1;
00057         m_seed = seed;
00058         SetStartVector();
00059 }
00060 
00061 SCA_RandomNumberGenerator::~SCA_RandomNumberGenerator() {
00062         /* intentionally empty */
00063 }
00064 
00065 void SCA_RandomNumberGenerator::SetStartVector(void) {
00066         /* setting initial seeds to mt[N] using         */
00067     /* the generator Line 25 of Table 1 in          */
00068     /* [KNUTH 1981, The Art of Computer Programming */
00069     /*    Vol. 2 (2nd Ed.), pp102]                  */
00070     mt[0] = m_seed & 0xffffffff;
00071     for (mti = 1; mti < N; mti++)
00072         mt[mti] = (69069 * mt[mti-1]) & 0xffffffff;
00073 }
00074 
00075 long SCA_RandomNumberGenerator::GetSeed() { return m_seed; }
00076 void SCA_RandomNumberGenerator::SetSeed(long newseed) 

00077 { 
00078         m_seed = newseed;
00079         SetStartVector();
00080 }
00081 
00085 unsigned long SCA_RandomNumberGenerator::Draw() {
00086     static unsigned long mag01[2] = { 0x0, MATRIX_A };
00087     /* mag01[x] = x * MATRIX_A  for x=0,1 */
00088 
00089     unsigned long y;
00090 
00091     if (mti >= N) { /* generate N words at one time */
00092         int kk;
00093 
00094         /* I set this in the constructor, so it is always satisfied ! */
00095 //          if (mti == N+1)   /* if sgenrand() has not been called, */
00096 //              GEN_srand(4357); /* a default initial seed is used   */
00097         
00098         for (kk = 0; kk < N - M; kk++) {
00099             y = (mt[kk] & UPPER_MASK) | (mt[kk+1] & LOWER_MASK);
00100             mt[kk] = mt[kk+M] ^ (y >> 1) ^ mag01[y & 0x1];
00101         }
00102         for (; kk < N-1; kk++) {
00103             y = (mt[kk] & UPPER_MASK) | (mt[kk+1] & LOWER_MASK);
00104             mt[kk] = mt[kk+(M-N)] ^ (y >> 1) ^ mag01[y & 0x1];
00105         }
00106         y = (mt[N-1] & UPPER_MASK) | (mt[0] & LOWER_MASK);
00107         mt[N-1] = mt[M-1] ^ (y >> 1) ^ mag01[y & 0x1];
00108 
00109         mti = 0;
00110     }
00111   
00112     y = mt[mti++];
00113     y ^= TEMPERING_SHIFT_U(y);
00114     y ^= TEMPERING_SHIFT_S(y) & TEMPERING_MASK_B;
00115     y ^= TEMPERING_SHIFT_T(y) & TEMPERING_MASK_C;
00116     y ^= TEMPERING_SHIFT_L(y);
00117 
00118     return y;
00119 }
00120 
00121 float SCA_RandomNumberGenerator::DrawFloat() {
00122         return ( (float) Draw()/ (unsigned long) 0xffffffff );
00123 }
00124 
00125 /* eof */

Generated at Thu Feb 1 13:03:11 2001 for Ketsji Game Engine by doxygen1.2.3 written by Dimitri van Heesch, © 1997-2000