Go to the documentation of this file.00001
00006
00007
00008
00009
00010
00011
00012
00013
00014
00070 #include "random.h"
00071
00072
00073
00074
00075
00076
00077
00078
00079
00080
00081
00082
00083
00084
00085
00086
00087
00088
00089
00090
00091
00092
00093
00094
00095
00096
00097
00098
00099
00100
00101
00102
00103
00104
00105
00106
00107
00108
00109
00110
00111
00112
00113
00114
00115 #include <stdio.h>
00116 #include <string.h>
00117
00118
00119 #define N 624
00120 #define M 397
00121 #define MATRIX_A VL_UINT32_C(0x9908b0df)
00122 #define UPPER_MASK VL_UINT32_C(0x80000000)
00123 #define LOWER_MASK VL_UINT32_C(0x7fffffff)
00124
00125
00126
00131 void
00132 vl_rand_init (VlRand * self)
00133 {
00134 memset (self->mt, 0, sizeof(self->mt[0]) * N) ;
00135 self->mti = N + 1 ;
00136 }
00137
00143 void
00144 vl_rand_seed (VlRand * self, vl_uint32 s)
00145 {
00146 #define mti self->mti
00147 #define mt self->mt
00148 mt[0]= s & VL_UINT32_C(0xffffffff);
00149 for (mti=1; mti<N; mti++) {
00150 mt[mti] =
00151 (VL_UINT32_C(1812433253) * (mt[mti-1] ^ (mt[mti-1] >> 30)) + mti);
00152
00153
00154
00155
00156 mt[mti] &= VL_UINT32_C(0xffffffff);
00157
00158 }
00159 #undef mti
00160 #undef mt
00161 }
00162
00169 void
00170 vl_rand_seed_by_array (VlRand * self, vl_uint32 const key [], vl_size keySize)
00171 {
00172 #define mti self->mti
00173 #define mt self->mt
00174 int i, j, k;
00175 vl_rand_seed (self, VL_UINT32_C(19650218));
00176 i=1; j=0;
00177 k = (N > keySize ? N : (int)keySize);
00178 for (; k; k--) {
00179 mt[i] = (mt[i] ^ ((mt[i-1] ^ (mt[i-1] >> 30)) * VL_UINT32_C(1664525)))
00180 + key[j] + j;
00181 mt[i] &= VL_UINT32_C(0xffffffff);
00182 i++; j++;
00183 if (i>=N) { mt[0] = mt[N-1]; i=1; }
00184 if (j>=(signed)keySize) j=0;
00185 }
00186 for (k=N-1; k; k--) {
00187 mt[i] = (mt[i] ^ ((mt[i-1] ^ (mt[i-1] >> 30)) * VL_UINT32_C(1566083941)))
00188 - i;
00189 mt[i] &= VL_UINT32_C(0xffffffff) ;
00190 i++;
00191 if (i>=N) { mt[0] = mt[N-1]; i=1; }
00192 }
00193
00194 mt[0] = VL_UINT32_C(0x80000000);
00195 #undef mti
00196 #undef mt
00197 }
00198
00207 void
00208 vl_rand_permute_indexes (VlRand *self, vl_index *array, vl_size size)
00209 {
00210 vl_index i, j, tmp;
00211 for (i = size - 1 ; i > 0; i--) {
00212
00213 j = (vl_int) vl_rand_uindex (self, i + 1) ;
00214 tmp = array[i] ; array[i] = array[j] ; array[j] = tmp ;
00215 }
00216 }
00217
00218
00224 vl_uint32
00225 vl_rand_uint32 (VlRand * self)
00226 {
00227 vl_uint32 y;
00228 static vl_uint32 mag01[2]={VL_UINT32_C(0x0), MATRIX_A};
00229
00230
00231 #define mti self->mti
00232 #define mt self->mt
00233
00234 if (mti >= N) {
00235 int kk;
00236
00237 if (mti == N+1)
00238 vl_rand_seed (self, VL_UINT32_C(5489));
00239
00240 for (kk=0;kk<N-M;kk++) {
00241 y = (mt[kk]&UPPER_MASK)|(mt[kk+1]&LOWER_MASK);
00242 mt[kk] = mt[kk+M] ^ (y >> 1) ^ mag01[y & VL_UINT32_C(0x1)];
00243 }
00244 for (;kk<N-1;kk++) {
00245 y = (mt[kk]&UPPER_MASK)|(mt[kk+1]&LOWER_MASK);
00246 mt[kk] = mt[kk+(M-N)] ^ (y >> 1) ^ mag01[y & VL_UINT32_C(0x1)];
00247 }
00248 y = (mt[N-1]&UPPER_MASK)|(mt[0]&LOWER_MASK);
00249 mt[N-1] = mt[M-1] ^ (y >> 1) ^ mag01[y & VL_UINT32_C(0x1)];
00250
00251 mti = 0;
00252 }
00253
00254 y = mt[mti++];
00255
00256
00257 y ^= (y >> 11);
00258 y ^= (y << 7) & VL_UINT32_C(0x9d2c5680);
00259 y ^= (y << 15) & VL_UINT32_C(0xefc60000);
00260 y ^= (y >> 18);
00261
00262 return (vl_uint32)y;
00263
00264 #undef mti
00265 #undef mt
00266 }