00001 #include <stdlib.h> 00002 #include <math.h> 00003 00004 #include "rand.h" 00005 00006 unsigned long IBM; 00007 long cur_rand; 00008 unsigned long rand_history[1280]; 00009 00010 00011 void sgenrand(unsigned long seed); 00012 double genrand(); 00013 00014 00015 void my_srand(long seed) { 00016 int i; 00017 srand(seed); 00018 IBM = seed; 00019 for (i = 0; i < 1280; i++) { 00020 rand_history[i] = rand() * rand(); 00021 } 00022 sgenrand(seed); 00023 cur_rand = 0; 00024 } 00025 00026 //this is the IBM random number generator reimplmented in C, as I under stand it 00027 double my_rand1(void) { 00028 IBM = IBM * 65549; 00029 //don't need the addition step because am using unsigned integers 00030 // if (IBM < 0) { 00031 // IBM = IBM + 2147483647 + 1; 00032 // } 00033 //2.3e-10 is 1/2^32 00034 return 2.328306436538696e-10 * (double)IBM; 00035 } 00036 00037 //my tausworth random number generator 00038 double my_rand2(void) { 00039 cur_rand--; 00040 if (cur_rand < 0) 00041 cur_rand += 1280; 00042 rand_history[cur_rand] = rand_history[ (cur_rand + 1279) % 1280 ] ^ rand_history[ (cur_rand + 418) % 1280] ; 00043 // return 2.328306436538696e-10 * (double)rand_history[cur_rand]; 00044 return (double)rand_history[cur_rand] / 4294967295.0; 00045 } 00046 00047 00048 double my_rand3(void) { 00049 return genrand(); 00050 } 00051 00052 double my_rand4(void) { 00053 return (double)rand() / RAND_MAX; 00054 } 00055 00056 00057 00058 00059 //GPL code for the MT19937 random number generator 00060 //this can be removed if open source code is not allowed in 00061 //the HW 00062 00063 /* A C-program for MT19937: Real number version([0,1)-interval) */ 00064 /* (1999/10/28) */ 00065 /* genrand() generates one pseudorandom real number (double) */ 00066 /* which is uniformly distributed on [0,1)-interval, for each */ 00067 /* call. sgenrand(seed) sets initial values to the working area */ 00068 /* of 624 words. Before genrand(), sgenrand(seed) must be */ 00069 /* called once. (seed is any 32-bit integer.) */ 00070 /* Integer generator is obtained by modifying two lines. */ 00071 /* Coded by Takuji Nishimura, considering the suggestions by */ 00072 /* Topher Cooper and Marc Rieffel in July-Aug. 1997. */ 00073 00074 /* This library is free software; you can redistribute it and/or */ 00075 /* modify it under the terms of the GNU Library General Public */ 00076 /* License as published by the Free Software Foundation; either */ 00077 /* version 2 of the License, or (at your option) any later */ 00078 /* version. */ 00079 /* This library is distributed in the hope that it will be useful, */ 00080 /* but WITHOUT ANY WARRANTY; without even the implied warranty of */ 00081 /* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. */ 00082 /* See the GNU Library General Public License for more details. */ 00083 /* You should have received a copy of the GNU Library General */ 00084 /* Public License along with this library; if not, write to the */ 00085 /* Free Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA */ 00086 /* 02111-1307 USA */ 00087 00088 /* Copyright (C) 1997, 1999 Makoto Matsumoto and Takuji Nishimura. */ 00089 /* Any feedback is very welcome. For any question, comments, */ 00090 /* see http://www.math.keio.ac.jp/matumoto/emt.html or email */ 00091 /* matumoto@math.keio.ac.jp */ 00092 00093 /* REFERENCE */ 00094 /* M. Matsumoto and T. Nishimura, */ 00095 /* "Mersenne Twister: A 623-Dimensionally Equidistributed Uniform */ 00096 /* Pseudo-Random Number Generator", */ 00097 /* ACM Transactions on Modeling and Computer Simulation, */ 00098 /* Vol. 8, No. 1, January 1998, pp 3--30. */ 00099 00100 #include<stdio.h> 00101 00102 /* Period parameters */ 00103 #define N 624 00104 #define M 397 00105 #define MATRIX_A 0x9908b0df /* constant vector a */ 00106 #define UPPER_MASK 0x80000000 /* most significant w-r bits */ 00107 #define LOWER_MASK 0x7fffffff /* least significant r bits */ 00108 00109 /* Tempering parameters */ 00110 #define TEMPERING_MASK_B 0x9d2c5680 00111 #define TEMPERING_MASK_C 0xefc60000 00112 #define TEMPERING_SHIFT_U(y) (y >> 11) 00113 #define TEMPERING_SHIFT_S(y) (y << 7) 00114 #define TEMPERING_SHIFT_T(y) (y << 15) 00115 #define TEMPERING_SHIFT_L(y) (y >> 18) 00116 00117 static unsigned long mt[N]; /* the array for the state vector */ 00118 static int mti=N+1; /* mti==N+1 means mt[N] is not initialized */ 00119 00120 /* Initializing the array with a seed */ 00121 void 00122 sgenrand(unsigned long seed) { 00123 int i; 00124 00125 for (i=0;i<N;i++) { 00126 mt[i] = seed & 0xffff0000; 00127 seed = 69069 * seed + 1; 00128 mt[i] |= (seed & 0xffff0000) >> 16; 00129 seed = 69069 * seed + 1; 00130 } 00131 mti = N; 00132 } 00133 00134 /* Initialization by "sgenrand()" is an example. Theoretically, */ 00135 /* there are 2^19937-1 possible states as an intial state. */ 00136 /* This function allows to choose any of 2^19937-1 ones. */ 00137 /* Essential bits in "seed_array[]" is following 19937 bits: */ 00138 /* (seed_array[0]&UPPER_MASK), seed_array[1], ..., seed_array[N-1]. */ 00139 /* (seed_array[0]&LOWER_MASK) is discarded. */ 00140 /* Theoretically, */ 00141 /* (seed_array[0]&UPPER_MASK), seed_array[1], ..., seed_array[N-1] */ 00142 /* can take any values except all zeros. */ 00143 void 00144 lsgenrand(unsigned long seed_array[]) 00145 00146 /* the length of seed_array[] must be at least N */ 00147 { 00148 int i; 00149 00150 for (i=0;i<N;i++) 00151 mt[i] = seed_array[i]; 00152 mti=N; 00153 } 00154 00155 double /* generating reals */ 00156 /* unsigned long */ /* for integer generation */ 00157 genrand() 00158 { 00159 unsigned long y; 00160 static unsigned long mag01[2]={0x0, MATRIX_A}; 00161 /* mag01[x] = x * MATRIX_A for x=0,1 */ 00162 00163 if (mti >= N) { /* generate N words at one time */ 00164 int kk; 00165 00166 if (mti == N+1) /* if sgenrand() has not been called, */ 00167 sgenrand(4357); /* a default initial seed is used */ 00168 00169 for (kk=0;kk<N-M;kk++) { 00170 y = (mt[kk]&UPPER_MASK)|(mt[kk+1]&LOWER_MASK); 00171 mt[kk] = mt[kk+M] ^ (y >> 1) ^ mag01[y & 0x1]; 00172 } 00173 for (;kk<N-1;kk++) { 00174 y = (mt[kk]&UPPER_MASK)|(mt[kk+1]&LOWER_MASK); 00175 mt[kk] = mt[kk+(M-N)] ^ (y >> 1) ^ mag01[y & 0x1]; 00176 } 00177 y = (mt[N-1]&UPPER_MASK)|(mt[0]&LOWER_MASK); 00178 mt[N-1] = mt[M-1] ^ (y >> 1) ^ mag01[y & 0x1]; 00179 00180 mti = 0; 00181 } 00182 00183 y = mt[mti++]; 00184 y ^= TEMPERING_SHIFT_U(y); 00185 y ^= TEMPERING_SHIFT_S(y) & TEMPERING_MASK_B; 00186 y ^= TEMPERING_SHIFT_T(y) & TEMPERING_MASK_C; 00187 y ^= TEMPERING_SHIFT_L(y); 00188 00189 return ( (double)y * 2.3283064365386963e-10 ); /* reals: [0,1)-interval */ 00190 /* return y; */ /* for integer generation */ 00191 }