comparison liboctave/randmtzig.c @ 10317:42d098307c30

untabify additional source files
author John W. Eaton <jwe@octave.org>
date Thu, 11 Feb 2010 13:30:42 -0500
parents 4c0cdbe0acca
children fd0a3ac60b0e
comparison
equal deleted inserted replaced
10316:9966f1f71c32 10317:42d098307c30
211 j = 0; 211 j = 0;
212 k = (MT_N > key_length ? MT_N : key_length); 212 k = (MT_N > key_length ? MT_N : key_length);
213 for (; k; k--) 213 for (; k; k--)
214 { 214 {
215 state[i] = (state[i] ^ ((state[i-1] ^ (state[i-1] >> 30)) * 1664525UL)) 215 state[i] = (state[i] ^ ((state[i-1] ^ (state[i-1] >> 30)) * 1664525UL))
216 + init_key[j] + j; /* non linear */ 216 + init_key[j] + j; /* non linear */
217 state[i] &= 0xffffffffUL; /* for WORDSIZE > 32 machines */ 217 state[i] &= 0xffffffffUL; /* for WORDSIZE > 32 machines */
218 i++; 218 i++;
219 j++; 219 j++;
220 if (i >= MT_N) 220 if (i >= MT_N)
221 { 221 {
222 state[0] = state[MT_N-1]; 222 state[0] = state[MT_N-1];
223 i = 1; 223 i = 1;
224 } 224 }
225 if (j >= key_length) 225 if (j >= key_length)
226 j = 0; 226 j = 0;
227 } 227 }
228 for (k = MT_N - 1; k; k--) 228 for (k = MT_N - 1; k; k--)
229 { 229 {
230 state[i] = (state[i] ^ ((state[i-1] ^ (state[i-1] >> 30)) * 1566083941UL)) 230 state[i] = (state[i] ^ ((state[i-1] ^ (state[i-1] >> 30)) * 1566083941UL))
231 - i; /* non linear */ 231 - i; /* non linear */
232 state[i] &= 0xffffffffUL; /* for WORDSIZE > 32 machines */ 232 state[i] &= 0xffffffffUL; /* for WORDSIZE > 32 machines */
233 i++; 233 i++;
234 if (i >= MT_N) 234 if (i >= MT_N)
235 { 235 {
236 state[0] = state[MT_N-1]; 236 state[0] = state[MT_N-1];
237 i = 1; 237 i = 1;
238 } 238 }
239 } 239 }
240 240
241 state[0] = 0x80000000UL; /* MSB is 1; assuring non-zero initial array */ 241 state[0] = 0x80000000UL; /* MSB is 1; assuring non-zero initial array */
242 left = 1; 242 left = 1;
243 initf = 1; 243 initf = 1;
251 251
252 /* Look for entropy in /dev/urandom */ 252 /* Look for entropy in /dev/urandom */
253 FILE* urandom =fopen("/dev/urandom", "rb"); 253 FILE* urandom =fopen("/dev/urandom", "rb");
254 if (urandom) 254 if (urandom)
255 { 255 {
256 while (n < MT_N) 256 while (n < MT_N)
257 { 257 {
258 unsigned char word[4]; 258 unsigned char word[4];
259 if (fread(word, 4, 1, urandom) != 1) 259 if (fread(word, 4, 1, urandom) != 1)
260 break; 260 break;
261 entropy[n++] = word[0]+(word[1]<<8)+(word[2]<<16)+(word[3]<<24); 261 entropy[n++] = word[0]+(word[1]<<8)+(word[2]<<16)+(word[3]<<24);
262 } 262 }
263 fclose(urandom); 263 fclose(urandom);
264 } 264 }
265 265
266 /* If there isn't enough entropy, gather some from various sources */ 266 /* If there isn't enough entropy, gather some from various sources */
267 if (n < MT_N) 267 if (n < MT_N)
268 entropy[n++] = time(NULL); /* Current time in seconds */ 268 entropy[n++] = time(NULL); /* Current time in seconds */
269 if (n < MT_N) 269 if (n < MT_N)
270 entropy[n++] = clock(); /* CPU time used (usec) */ 270 entropy[n++] = clock(); /* CPU time used (usec) */
271 #ifdef HAVE_GETTIMEOFDAY 271 #ifdef HAVE_GETTIMEOFDAY
272 if (n < MT_N) 272 if (n < MT_N)
273 { 273 {
274 struct timeval tv; 274 struct timeval tv;
275 if (gettimeofday(&tv, NULL) != -1) 275 if (gettimeofday(&tv, NULL) != -1)
276 entropy[n++] = tv.tv_usec; /* Fractional part of current time */ 276 entropy[n++] = tv.tv_usec; /* Fractional part of current time */
277 } 277 }
278 #endif 278 #endif
279 /* Send all the entropy into the initial state vector */ 279 /* Send all the entropy into the initial state vector */
280 oct_init_by_array(entropy,n); 280 oct_init_by_array(entropy,n);
281 } 281 }
621 const uint32_t rabs = r&LMASK; 621 const uint32_t rabs = r&LMASK;
622 const int idx = (int)(r&0xFF); 622 const int idx = (int)(r&0xFF);
623 const double x = ((int32_t)r) * wi[idx]; 623 const double x = ((int32_t)r) * wi[idx];
624 if (rabs < ki[idx]) 624 if (rabs < ki[idx])
625 #endif /* ALLBITS */ 625 #endif /* ALLBITS */
626 return x; /* 99.3% of the time we return here 1st try */ 626 return x; /* 99.3% of the time we return here 1st try */
627 else if (idx == 0) 627 else if (idx == 0)
628 { 628 {
629 /* As stated in Marsaglia and Tsang 629 /* As stated in Marsaglia and Tsang
630 * 630 *
631 * For the normal tail, the method of Marsaglia[5] provides: 631 * For the normal tail, the method of Marsaglia[5] provides:
632 * generate x = -ln(U_1)/r, y = -ln(U_2), until y+y > x*x, 632 * generate x = -ln(U_1)/r, y = -ln(U_2), until y+y > x*x,
633 * then return r+x. Except that r+x is always in the positive 633 * then return r+x. Except that r+x is always in the positive
634 * tail!!!! Any thing random might be used to determine the 634 * tail!!!! Any thing random might be used to determine the
635 * sign, but as we already have r we might as well use it 635 * sign, but as we already have r we might as well use it
636 * 636 *
637 * [PAK] but not the bottom 8 bits, since they are all 0 here! 637 * [PAK] but not the bottom 8 bits, since they are all 0 here!
638 */ 638 */
639 double xx, yy; 639 double xx, yy;
640 do 640 do
641 { 641 {
642 xx = - ZIGGURAT_NOR_INV_R * log (RANDU); 642 xx = - ZIGGURAT_NOR_INV_R * log (RANDU);
643 yy = - log (RANDU); 643 yy = - log (RANDU);
644 } 644 }
645 while ( yy+yy <= xx*xx); 645 while ( yy+yy <= xx*xx);
646 return (rabs&0x100 ? -ZIGGURAT_NOR_R-xx : ZIGGURAT_NOR_R+xx); 646 return (rabs&0x100 ? -ZIGGURAT_NOR_R-xx : ZIGGURAT_NOR_R+xx);
647 } 647 }
648 else if ((fi[idx-1] - fi[idx]) * RANDU + fi[idx] < exp(-0.5*x*x)) 648 else if ((fi[idx-1] - fi[idx]) * RANDU + fi[idx] < exp(-0.5*x*x))
649 return x; 649 return x;
650 } 650 }
651 } 651 }
652 652
653 double 653 double
654 oct_rande (void) 654 oct_rande (void)
660 { 660 {
661 ZIGINT ri = ERANDI; 661 ZIGINT ri = ERANDI;
662 const int idx = (int)(ri & 0xFF); 662 const int idx = (int)(ri & 0xFF);
663 const double x = ri * we[idx]; 663 const double x = ri * we[idx];
664 if (ri < ke[idx]) 664 if (ri < ke[idx])
665 return x; // 98.9% of the time we return here 1st try 665 return x; // 98.9% of the time we return here 1st try
666 else if (idx == 0) 666 else if (idx == 0)
667 { 667 {
668 /* As stated in Marsaglia and Tsang 668 /* As stated in Marsaglia and Tsang
669 * 669 *
670 * For the exponential tail, the method of Marsaglia[5] provides: 670 * For the exponential tail, the method of Marsaglia[5] provides:
671 * x = r - ln(U); 671 * x = r - ln(U);
672 */ 672 */
673 return ZIGGURAT_EXP_R - log(RANDU); 673 return ZIGGURAT_EXP_R - log(RANDU);
674 } 674 }
675 else if ((fe[idx-1] - fe[idx]) * RANDU + fe[idx] < exp(-x)) 675 else if ((fe[idx-1] - fe[idx]) * RANDU + fe[idx] < exp(-x))
676 return x; 676 return x;
677 } 677 }
678 } 678 }
679 679
680 /* Array generators */ 680 /* Array generators */
681 void 681 void