00001 /* 00002 A C-program for MT19937, with initialization improved 2002/1/26. 00003 Coded by Takuji Nishimura and Makoto Matsumoto. 00004 Modified to be a C++ class by Daniel Dunbar. 00005 00006 Before using, initialize the state by using init_genrand(seed) 00007 or init_by_array(init_key, key_length). 00008 00009 Copyright (C) 1997 - 2002, Makoto Matsumoto and Takuji Nishimura, 00010 All rights reserved. 00011 00012 Redistribution and use in source and binary forms, with or without 00013 modification, are permitted provided that the following conditions 00014 are met: 00015 00016 1. Redistributions of source code must retain the above copyright 00017 notice, this list of conditions and the following disclaimer. 00018 00019 2. Redistributions in binary form must reproduce the above copyright 00020 notice, this list of conditions and the following disclaimer in the 00021 documentation and/or other materials provided with the distribution. 00022 00023 3. The names of its contributors may not be used to endorse or promote 00024 products derived from this software without specific prior written 00025 permission. 00026 00027 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 00028 "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 00029 LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 00030 A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR 00031 CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, 00032 EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, 00033 PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR 00034 PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF 00035 LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING 00036 NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS 00037 SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 00038 00039 00040 Any feedback is very welcome. 00041 http://www.math.sci.hiroshima-u.ac.jp/~m-mat/MT/emt.html 00042 email: m-mat @ math.sci.hiroshima-u.ac.jp (remove space) 00043 */ 00044 00045 #include "klee/Internal/ADT/RNG.h" 00046 00047 using namespace klee; 00048 00049 /* initializes mt[N] with a seed */ 00050 RNG::RNG(unsigned int s) { 00051 seed(s); 00052 } 00053 00054 void RNG::seed(unsigned int s) { 00055 mt[0]= s & 0xffffffffUL; 00056 for (mti=1; mti<N; mti++) { 00057 mt[mti] = 00058 (1812433253UL * (mt[mti-1] ^ (mt[mti-1] >> 30)) + mti); 00059 /* See Knuth TAOCP Vol2. 3rd Ed. P.106 for multiplier. */ 00060 /* In the previous versions, MSBs of the seed affect */ 00061 /* only MSBs of the array mt[]. */ 00062 /* 2002/01/09 modified by Makoto Matsumoto */ 00063 mt[mti] &= 0xffffffffUL; 00064 /* for >32 bit machines */ 00065 } 00066 } 00067 00068 /* generates a random number on [0,0xffffffff]-interval */ 00069 unsigned int RNG::getInt32() { 00070 unsigned int y; 00071 static unsigned int mag01[2]={0x0UL, MATRIX_A}; 00072 /* mag01[x] = x * MATRIX_A for x=0,1 */ 00073 00074 if (mti >= N) { /* generate N words at one time */ 00075 int kk; 00076 00077 for (kk=0;kk<N-M;kk++) { 00078 y = (mt[kk]&UPPER_MASK)|(mt[kk+1]&LOWER_MASK); 00079 mt[kk] = mt[kk+M] ^ (y >> 1) ^ mag01[y & 0x1UL]; 00080 } 00081 for (;kk<N-1;kk++) { 00082 y = (mt[kk]&UPPER_MASK)|(mt[kk+1]&LOWER_MASK); 00083 mt[kk] = mt[kk+(M-N)] ^ (y >> 1) ^ mag01[y & 0x1UL]; 00084 } 00085 y = (mt[N-1]&UPPER_MASK)|(mt[0]&LOWER_MASK); 00086 mt[N-1] = mt[M-1] ^ (y >> 1) ^ mag01[y & 0x1UL]; 00087 00088 mti = 0; 00089 } 00090 00091 y = mt[mti++]; 00092 00093 /* Tempering */ 00094 y ^= (y >> 11); 00095 y ^= (y << 7) & 0x9d2c5680UL; 00096 y ^= (y << 15) & 0xefc60000UL; 00097 y ^= (y >> 18); 00098 00099 return y; 00100 } 00101 00102 /* generates a random number on [0,0x7fffffff]-interval */ 00103 int RNG::getInt31() { 00104 return (int)(getInt32()>>1); 00105 } 00106 00107 /* generates a random number on [0,1]-real-interval */ 00108 double RNG::getDoubleLR() { 00109 return getInt32()*(1.0/4294967295.0); 00110 /* divided by 2^32-1 */ 00111 } 00112 00113 /* generates a random number on [0,1)-real-interval */ 00114 double RNG::getDoubleL() { 00115 return getInt32()*(1.0/4294967296.0); 00116 /* divided by 2^32 */ 00117 } 00118 00119 /* generates a random number on (0,1)-real-interval */ 00120 double RNG::getDouble() { 00121 return (((double)getInt32()) + 0.5)*(1.0/4294967296.0); 00122 /* divided by 2^32 */ 00123 } 00124 00125 float RNG::getFloatLR() { 00126 return getInt32()*(1.0f/4294967295.0f); 00127 /* divided by 2^32-1 */ 00128 } 00129 float RNG::getFloatL() { 00130 return getInt32()*(1.0f/4294967296.0f); 00131 /* divided by 2^32 */ 00132 } 00133 float RNG::getFloat() { 00134 return (getInt32() + 0.5f)*(1.0f/4294967296.0f); 00135 /* divided by 2^32 */ 00136 } 00137 00138 bool RNG::getBool() { 00139 unsigned bits = getInt32(); 00140 bits ^= bits >> 16; 00141 bits ^= bits >> 8; 00142 bits ^= bits >> 4; 00143 bits ^= bits >> 2; 00144 bits ^= bits >> 1; 00145 return bits&1; 00146 }
1.5.8