| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
#include <esl_config.h> |
| 27 |
|
| 28 |
#include <stdlib.h> |
| 29 |
#include <stdint.h> |
| 30 |
#include <stdio.h> |
| 31 |
#include <string.h> |
| 32 |
#include <ctype.h> |
| 33 |
#include <math.h> |
| 34 |
#include <time.h> |
| 35 |
#ifdef HAVE_UNISTD_H |
| 36 |
#include <unistd.h> |
| 37 |
#endif |
| 38 |
|
| 39 |
#include "easel.h" |
| 40 |
#include "esl_random.h" |
| 41 |
|
| 42 |
static uint32_t choose_arbitrary_seed(void); |
| 43 |
static uint32_t knuth (ESL_RANDOMNESS *r); |
| 44 |
static uint32_t mersenne_twister (ESL_RANDOMNESS *r); |
| 45 |
static void mersenne_seed_table(ESL_RANDOMNESS *r, uint32_t seed); |
| 46 |
static void mersenne_fill_table(ESL_RANDOMNESS *r); |
| 47 |
|
| 48 |
|
| 49 |
|
| 50 |
|
| 51 |
|
| 52 |
|
| 53 |
|
| 54 |
|
| 55 |
|
| 56 |
|
| 57 |
|
| 58 |
|
| 59 |
|
| 60 |
|
| 61 |
|
| 62 |
|
| 63 |
|
| 64 |
|
| 65 |
|
| 66 |
|
| 67 |
|
| 68 |
|
| 69 |
|
| 70 |
|
| 71 |
|
| 72 |
|
| 73 |
|
| 74 |
|
| 75 |
|
| 76 |
|
| 77 |
|
| 78 |
|
| 79 |
|
| 80 |
|
| 81 |
|
| 82 |
|
| 83 |
|
| 84 |
|
| 85 |
|
| 86 |
|
| 87 |
|
| 88 |
|
| 89 |
ESL_RANDOMNESS * |
| 90 |
esl_randomness_Create(uint32_t seed) |
| 91 |
{ |
| 92 |
ESL_RANDOMNESS *r = NULL; |
| 93 |
int status; |
| 94 |
|
| 95 |
ESL_ALLOC(r, sizeof(ESL_RANDOMNESS)); |
| 96 |
r->type = eslRND_MERSENNE; |
| 97 |
r->mti = 0; |
| 98 |
r->x = 0; |
| 99 |
r->seed = 0; |
| 100 |
esl_randomness_Init(r, seed); |
| 101 |
return r; |
| 102 |
|
| 103 |
ERROR: |
| 104 |
return NULL; |
| 105 |
} |
| 106 |
|
| 107 |
|
| 108 |
|
| 109 |
|
| 110 |
|
| 111 |
|
| 112 |
|
| 113 |
|
| 114 |
|
| 115 |
|
| 116 |
|
| 117 |
|
| 118 |
|
| 119 |
|
| 120 |
|
| 121 |
|
| 122 |
|
| 123 |
|
| 124 |
|
| 125 |
|
| 126 |
|
| 127 |
|
| 128 |
|
| 129 |
|
| 130 |
|
| 131 |
|
| 132 |
|
| 133 |
|
| 134 |
|
| 135 |
|
| 136 |
|
| 137 |
|
| 138 |
|
| 139 |
|
| 140 |
|
| 141 |
|
| 142 |
|
| 143 |
|
| 144 |
|
| 145 |
|
| 146 |
|
| 147 |
|
| 148 |
|
| 149 |
ESL_RANDOMNESS * |
| 150 |
esl_randomness_CreateFast(uint32_t seed) |
| 151 |
{ |
| 152 |
ESL_RANDOMNESS *r = NULL; |
| 153 |
int status; |
| 154 |
|
| 155 |
ESL_ALLOC(r, sizeof(ESL_RANDOMNESS)); |
| 156 |
r->type = eslRND_FAST; |
| 157 |
r->mti = 0; |
| 158 |
r->x = 0; |
| 159 |
r->seed = 0; |
| 160 |
esl_randomness_Init(r, seed); |
| 161 |
return r; |
| 162 |
|
| 163 |
ERROR: |
| 164 |
return NULL; |
| 165 |
} |
| 166 |
|
| 167 |
|
| 168 |
|
| 169 |
|
| 170 |
|
| 171 |
|
| 172 |
|
| 173 |
|
| 174 |
|
| 175 |
|
| 176 |
|
| 177 |
|
| 178 |
|
| 179 |
|
| 180 |
|
| 181 |
|
| 182 |
|
| 183 |
|
| 184 |
|
| 185 |
|
| 186 |
|
| 187 |
ESL_RANDOMNESS * |
| 188 |
esl_randomness_CreateTimeseeded(void) |
| 189 |
{ |
| 190 |
return esl_randomness_Create(0); |
| 191 |
} |
| 192 |
|
| 193 |
|
| 194 |
|
| 195 |
|
| 196 |
|
| 197 |
|
| 198 |
|
| 199 |
|
| 200 |
|
| 201 |
|
| 202 |
|
| 203 |
|
| 204 |
|
| 205 |
|
| 206 |
|
| 207 |
|
| 208 |
|
| 209 |
|
| 210 |
|
| 211 |
|
| 212 |
|
| 213 |
|
| 214 |
int |
| 215 |
esl_randomness_Init(ESL_RANDOMNESS *r, uint32_t seed) |
| 216 |
{ |
| 217 |
if (seed == 0) seed = choose_arbitrary_seed(); |
| 218 |
if (r->type == eslRND_MERSENNE) |
| 219 |
{ |
| 220 |
mersenne_seed_table(r, seed); |
| 221 |
mersenne_fill_table(r); |
| 222 |
} |
| 223 |
else |
| 224 |
{ |
| 225 |
r->seed = seed; |
| 226 |
r->x = esl_mix3(seed, 87654321, 12345678); |
| 227 |
if (r->x == 0) r->x = 42; |
| 228 |
} |
| 229 |
return eslOK; |
| 230 |
} |
| 231 |
|
| 232 |
|
| 233 |
|
| 234 |
|
| 235 |
|
| 236 |
|
| 237 |
uint32_t |
| 238 |
esl_randomness_GetSeed(const ESL_RANDOMNESS *r) |
| 239 |
{ |
| 240 |
return r->seed; |
| 241 |
} |
| 242 |
|
| 243 |
|
| 244 |
|
| 245 |
|
| 246 |
|
| 247 |
|
| 248 |
|
| 249 |
void |
| 250 |
esl_randomness_Destroy(ESL_RANDOMNESS *r) |
| 251 |
{ |
| 252 |
free(r); |
| 253 |
return; |
| 254 |
} |
| 255 |
|
| 256 |
|
| 257 |
|
| 258 |
|
| 259 |
|
| 260 |
|
| 261 |
|
| 262 |
|
| 263 |
|
| 264 |
|
| 265 |
|
| 266 |
|
| 267 |
|
| 268 |
|
| 269 |
|
| 270 |
|
| 271 |
|
| 272 |
|
| 273 |
|
| 274 |
|
| 275 |
|
| 276 |
|
| 277 |
|
| 278 |
|
| 279 |
|
| 280 |
|
| 281 |
|
| 282 |
|
| 283 |
double |
| 284 |
esl_random(ESL_RANDOMNESS *r) |
| 285 |
{ |
| 286 |
uint32_t x = (r->type == eslRND_MERSENNE) ? mersenne_twister(r) : knuth(r); |
| 287 |
return ((double) x / 4294967296.0); |
| 288 |
} |
| 289 |
|
| 290 |
|
| 291 |
|
| 292 |
|
| 293 |
|
| 294 |
|
| 295 |
|
| 296 |
|
| 297 |
|
| 298 |
uint32_t |
| 299 |
esl_random_uint32(ESL_RANDOMNESS *r) |
| 300 |
{ |
| 301 |
return (r->type == eslRND_MERSENNE) ? mersenne_twister(r) : knuth(r); |
| 302 |
} |
| 303 |
|
| 304 |
|
| 305 |
|
| 306 |
|
| 307 |
|
| 308 |
|
| 309 |
|
| 310 |
|
| 311 |
|
| 312 |
int |
| 313 |
esl_rnd_Roll(ESL_RANDOMNESS *r, int n) |
| 314 |
{ |
| 315 |
ESL_DASSERT1(( n>0 )); |
| 316 |
uint32_t factor = UINT32_MAX / (uint32_t) n; |
| 317 |
uint32_t u; |
| 318 |
|
| 319 |
do { u = esl_random_uint32(r) / factor; } while (u >= n); |
| 320 |
return (int) u; |
| 321 |
} |
| 322 |
|
| 323 |
|
| 324 |
static uint32_t |
| 325 |
knuth(ESL_RANDOMNESS *r) |
| 326 |
{ |
| 327 |
r->x *= 69069; |
| 328 |
r->x += 1; |
| 329 |
return r->x; |
| 330 |
} |
| 331 |
|
| 332 |
|
| 333 |
|
| 334 |
|
| 335 |
|
| 336 |
|
| 337 |
|
| 338 |
|
| 339 |
|
| 340 |
|
| 341 |
|
| 342 |
|
| 343 |
static uint32_t |
| 344 |
mersenne_twister(ESL_RANDOMNESS *r) |
| 345 |
{ |
| 346 |
uint32_t x; |
| 347 |
if (r->mti >= 624) mersenne_fill_table(r); |
| 348 |
|
| 349 |
x = r->mt[r->mti++]; |
| 350 |
x ^= (x >> 11); |
| 351 |
x ^= (x << 7) & 0x9d2c5680; |
| 352 |
x ^= (x << 15) & 0xefc60000; |
| 353 |
x ^= (x >> 18); |
| 354 |
return x; |
| 355 |
} |
| 356 |
|
| 357 |
|
| 358 |
|
| 359 |
|
| 360 |
|
| 361 |
|
| 362 |
|
| 363 |
|
| 364 |
|
| 365 |
|
| 366 |
|
| 367 |
|
| 368 |
|
| 369 |
static void |
| 370 |
mersenne_seed_table(ESL_RANDOMNESS *r, uint32_t seed) |
| 371 |
{ |
| 372 |
int z; |
| 373 |
|
| 374 |
r->seed = seed; |
| 375 |
r->mt[0] = seed; |
| 376 |
for (z = 1; z < 624; z++) |
| 377 |
r->mt[z] = 69069 * r->mt[z-1]; |
| 378 |
return; |
| 379 |
} |
| 380 |
|
| 381 |
|
| 382 |
|
| 383 |
|
| 384 |
|
| 385 |
|
| 386 |
static void |
| 387 |
mersenne_fill_table(ESL_RANDOMNESS *r) |
| 388 |
{ |
| 389 |
uint32_t y; |
| 390 |
int z; |
| 391 |
static uint32_t mag01[2] = { 0x0, 0x9908b0df }; |
| 392 |
|
| 393 |
for (z = 0; z < 227; z++) |
| 394 |
{ |
| 395 |
y = (r->mt[z] & 0x80000000) | (r->mt[z+1] & 0x7fffffff); |
| 396 |
r->mt[z] = r->mt[z+397] ^ (y>>1) ^ mag01[(int)(y & 0x1)]; |
| 397 |
} |
| 398 |
for (; z < 623; z++) |
| 399 |
{ |
| 400 |
y = (r->mt[z] & 0x80000000) | (r->mt[z+1] & 0x7fffffff); |
| 401 |
r->mt[z] = r->mt[z-227] ^ (y>>1) ^ mag01[(int)(y & 0x1)]; |
| 402 |
} |
| 403 |
y = (r->mt[623] & 0x80000000) | (r->mt[0] & 0x7fffffff); |
| 404 |
r->mt[623] = r->mt[396] ^ (y>>1) ^ mag01[(int)(y & 0x1)]; |
| 405 |
r->mti = 0; |
| 406 |
|
| 407 |
return; |
| 408 |
} |
| 409 |
|
| 410 |
|
| 411 |
|
| 412 |
|
| 413 |
|
| 414 |
|
| 415 |
|
| 416 |
static uint32_t |
| 417 |
choose_arbitrary_seed(void) |
| 418 |
{ |
| 419 |
uint32_t a = (uint32_t) time ((time_t *) NULL); |
| 420 |
uint32_t b = 87654321; |
| 421 |
uint32_t c = (uint32_t) clock(); |
| 422 |
uint32_t seed; |
| 423 |
#ifdef HAVE_GETPID |
| 424 |
b = (uint32_t) getpid(); |
| 425 |
#endif |
| 426 |
seed = esl_mix3(a,b,c); |
| 427 |
return (seed == 0) ? 42 : seed; |
| 428 |
} |
| 429 |
|
| 430 |
|
| 431 |
|
| 432 |
|
| 433 |
|
| 434 |
|
| 435 |
|
| 436 |
|
| 437 |
|
| 438 |
|
| 439 |
|
| 440 |
|
| 441 |
|
| 442 |
int |
| 443 |
esl_randomness_Dump(FILE *fp, ESL_RANDOMNESS *r) |
| 444 |
{ |
| 445 |
if (r->type == eslRND_FAST) |
| 446 |
{ |
| 447 |
fputs ("type = knuth\n", fp ); |
| 448 |
fprintf(fp, "state = %" PRIu32 "\n", r->x); |
| 449 |
fprintf(fp, "seed = %" PRIu32 "\n", r->seed); |
| 450 |
} |
| 451 |
else if (r->type == eslRND_MERSENNE) |
| 452 |
{ |
| 453 |
int i,j; |
| 454 |
|
| 455 |
fputs ("type = mersenne twister\n", fp ); |
| 456 |
fprintf(fp, "mti = %d (0..623)\n", r->mti); |
| 457 |
fprintf(fp, "mt[mti] = %" PRIu32 "\n", r->mt[r->mti]); |
| 458 |
|
| 459 |
fprintf(fp, "%6d: ", 0); |
| 460 |
for (i = 0, j=0; i < 624; i++) |
| 461 |
{ |
| 462 |
fprintf(fp, "%11" PRIu32 " ", r->mt[i]); |
| 463 |
if (++j == 20) { fprintf(fp, "\n%6d: ", i+1); j=0; } |
| 464 |
} |
| 465 |
fputs("\n", fp); |
| 466 |
} |
| 467 |
return eslOK; |
| 468 |
} |
| 469 |
|
| 470 |
|
| 471 |
|
| 472 |
|
| 473 |
|
| 474 |
|
| 475 |
|
| 476 |
|
| 477 |
|
| 478 |
|
| 479 |
|
| 480 |
|
| 481 |
|
| 482 |
double |
| 483 |
esl_rnd_UniformPositive(ESL_RANDOMNESS *r) |
| 484 |
{ |
| 485 |
double x; |
| 486 |
do { x = esl_random(r); } while (x == 0.0); |
| 487 |
return x; |
| 488 |
} |
| 489 |
|
| 490 |
|
| 491 |
|
| 492 |
|
| 493 |
|
| 494 |
|
| 495 |
|
| 496 |
|
| 497 |
|
| 498 |
|
| 499 |
|
| 500 |
|
| 501 |
|
| 502 |
|
| 503 |
|
| 504 |
|
| 505 |
|
| 506 |
|
| 507 |
|
| 508 |
|
| 509 |
|
| 510 |
|
| 511 |
|
| 512 |
|
| 513 |
|
| 514 |
|
| 515 |
|
| 516 |
|
| 517 |
|
| 518 |
double |
| 519 |
esl_rnd_Gaussian(ESL_RANDOMNESS *r, double mean, double stddev) |
| 520 |
{ |
| 521 |
long i; |
| 522 |
double snorm,u,s,ustar,aa,w,y,tt; |
| 523 |
|
| 524 |
|
| 525 |
|
| 526 |
|
| 527 |
static double a[32] = { |
| 528 |
0.0,3.917609E-2,7.841241E-2,0.11777,0.1573107,0.1970991,0.2372021,0.2776904, |
| 529 |
0.3186394,0.36013,0.4022501,0.4450965,0.4887764,0.5334097,0.5791322, |
| 530 |
0.626099,0.6744898,0.7245144,0.7764218,0.8305109,0.8871466,0.9467818, |
| 531 |
1.00999,1.077516,1.150349,1.229859,1.318011,1.417797,1.534121,1.67594, |
| 532 |
1.862732,2.153875 |
| 533 |
}; |
| 534 |
static double d[31] = { |
| 535 |
0.0,0.0,0.0,0.0,0.0,0.2636843,0.2425085,0.2255674,0.2116342,0.1999243, |
| 536 |
0.1899108,0.1812252,0.1736014,0.1668419,0.1607967,0.1553497,0.1504094, |
| 537 |
0.1459026,0.14177,0.1379632,0.1344418,0.1311722,0.128126,0.1252791, |
| 538 |
0.1226109,0.1201036,0.1177417,0.1155119,0.1134023,0.1114027,0.1095039 |
| 539 |
}; |
| 540 |
static double t[31] = { |
| 541 |
7.673828E-4,2.30687E-3,3.860618E-3,5.438454E-3,7.0507E-3,8.708396E-3, |
| 542 |
1.042357E-2,1.220953E-2,1.408125E-2,1.605579E-2,1.81529E-2,2.039573E-2, |
| 543 |
2.281177E-2,2.543407E-2,2.830296E-2,3.146822E-2,3.499233E-2,3.895483E-2, |
| 544 |
4.345878E-2,4.864035E-2,5.468334E-2,6.184222E-2,7.047983E-2,8.113195E-2, |
| 545 |
9.462444E-2,0.1123001,0.136498,0.1716886,0.2276241,0.330498,0.5847031 |
| 546 |
}; |
| 547 |
static double h[31] = { |
| 548 |
3.920617E-2,3.932705E-2,3.951E-2,3.975703E-2,4.007093E-2,4.045533E-2, |
| 549 |
4.091481E-2,4.145507E-2,4.208311E-2,4.280748E-2,4.363863E-2,4.458932E-2, |
| 550 |
4.567523E-2,4.691571E-2,4.833487E-2,4.996298E-2,5.183859E-2,5.401138E-2, |
| 551 |
5.654656E-2,5.95313E-2,6.308489E-2,6.737503E-2,7.264544E-2,7.926471E-2, |
| 552 |
8.781922E-2,9.930398E-2,0.11556,0.1404344,0.1836142,0.2790016,0.7010474 |
| 553 |
}; |
| 554 |
|
| 555 |
u = esl_rnd_UniformPositive(r); |
| 556 |
s = 0.0; |
| 557 |
if(u > 0.5) s = 1.0; |
| 558 |
u += (u-s); |
| 559 |
u = 32.0*u; |
| 560 |
i = (long) (u); |
| 561 |
if(i == 32) i = 31; |
| 562 |
if(i == 0) goto S100; |
| 563 |
|
| 564 |
|
| 565 |
|
| 566 |
ustar = u-(double)i; |
| 567 |
aa = a[i-1]; |
| 568 |
S40: |
| 569 |
if (ustar <= t[i-1]) goto S60; |
| 570 |
w = (ustar - t[i-1]) * h[i-1]; |
| 571 |
S50: |
| 572 |
|
| 573 |
|
| 574 |
|
| 575 |
y = aa+w; |
| 576 |
snorm = y; |
| 577 |
if(s == 1.0) snorm = -y; |
| 578 |
return (stddev*snorm + mean); |
| 579 |
S60: |
| 580 |
|
| 581 |
|
| 582 |
|
| 583 |
u = esl_rnd_UniformPositive(r); |
| 584 |
w = u*(a[i]-aa); |
| 585 |
tt = (0.5*w+aa)*w; |
| 586 |
goto S80; |
| 587 |
S70: |
| 588 |
tt = u; |
| 589 |
ustar = esl_rnd_UniformPositive(r); |
| 590 |
S80: |
| 591 |
if(ustar > tt) goto S50; |
| 592 |
u = esl_rnd_UniformPositive(r); |
| 593 |
if(ustar >= u) goto S70; |
| 594 |
ustar = esl_rnd_UniformPositive(r); |
| 595 |
goto S40; |
| 596 |
S100: |
| 597 |
|
| 598 |
|
| 599 |
|
| 600 |
i = 6; |
| 601 |
aa = a[31]; |
| 602 |
goto S120; |
| 603 |
S110: |
| 604 |
aa += d[i-1]; |
| 605 |
i += 1; |
| 606 |
ESL_DASSERT1(( i <= 31 )); |
| 607 |
S120: |
| 608 |
u += u; |
| 609 |
if(u < 1.0) goto S110; |
| 610 |
u -= 1.0; |
| 611 |
S140: |
| 612 |
w = u*d[i-1]; |
| 613 |
tt = (0.5*w+aa)*w; |
| 614 |
goto S160; |
| 615 |
S150: |
| 616 |
tt = u; |
| 617 |
S160: |
| 618 |
ustar = esl_rnd_UniformPositive(r); |
| 619 |
if(ustar > tt) goto S50; |
| 620 |
u = esl_rnd_UniformPositive(r); |
| 621 |
if(ustar >= u) goto S150; |
| 622 |
u = esl_rnd_UniformPositive(r); |
| 623 |
goto S140; |
| 624 |
} |
| 625 |
|
| 626 |
|
| 627 |
|
| 628 |
|
| 629 |
|
| 630 |
static double |
| 631 |
gamma_ahrens(ESL_RANDOMNESS *r, double a) |
| 632 |
{ |
| 633 |
double V; |
| 634 |
double X,Y; |
| 635 |
double test; |
| 636 |
|
| 637 |
do { |
| 638 |
do { |
| 639 |
Y = tan(eslCONST_PI * esl_random(r)); |
| 640 |
X = Y * sqrt(2.*a -1.) + a - 1.; |
| 641 |
} while (X <= 0.); |
| 642 |
|
| 643 |
V = esl_random(r); |
| 644 |
test = (1+Y*Y) * exp( (a-1.)* log(X/(a-1.)) - Y*sqrt(2.*a-1.)); |
| 645 |
} while (V > test); |
| 646 |
return X; |
| 647 |
} |
| 648 |
static double |
| 649 |
gamma_integer(ESL_RANDOMNESS *r, unsigned int a) |
| 650 |
{ |
| 651 |
int i; |
| 652 |
double U,X; |
| 653 |
|
| 654 |
U = 1.; |
| 655 |
for (i = 0; i < a; i++) |
| 656 |
U *= esl_rnd_UniformPositive(r); |
| 657 |
X = -log(U); |
| 658 |
|
| 659 |
return X; |
| 660 |
} |
| 661 |
static double |
| 662 |
gamma_fraction(ESL_RANDOMNESS *r, double a) |
| 663 |
{ |
| 664 |
double p, U, V, X, q; |
| 665 |
|
| 666 |
p = eslCONST_E / (a + eslCONST_E); |
| 667 |
do { |
| 668 |
U = esl_random(r); |
| 669 |
V = esl_rnd_UniformPositive(r); |
| 670 |
if (U < p) { |
| 671 |
X = pow(V, 1./a); |
| 672 |
q = exp(-X); |
| 673 |
} else { |
| 674 |
X = 1. - log(V); |
| 675 |
q = pow(X, a-1.); |
| 676 |
} |
| 677 |
U = esl_random(r); |
| 678 |
} while (U >= q); |
| 679 |
return X; |
| 680 |
} |
| 681 |
|
| 682 |
|
| 683 |
|
| 684 |
|
| 685 |
|
| 686 |
|
| 687 |
|
| 688 |
|
| 689 |
|
| 690 |
|
| 691 |
|
| 692 |
|
| 693 |
|
| 694 |
|
| 695 |
|
| 696 |
double |
| 697 |
esl_rnd_Gamma(ESL_RANDOMNESS *r, double a) |
| 698 |
{ |
| 699 |
double aint; |
| 700 |
|
| 701 |
ESL_DASSERT1(( a > 0. )); |
| 702 |
|
| 703 |
aint = floor(a); |
| 704 |
if (a == aint && a < 12.) |
| 705 |
return gamma_integer(r, (unsigned int) a); |
| 706 |
else if (a > 3.) |
| 707 |
return gamma_ahrens(r, a); |
| 708 |
else if (a < 1.) |
| 709 |
return gamma_fraction(r, a); |
| 710 |
else |
| 711 |
return gamma_integer(r, aint) + gamma_fraction(r, a-aint); |
| 712 |
} |
| 713 |
|
| 714 |
|
| 715 |
|
| 716 |
|
| 717 |
|
| 718 |
|
| 719 |
|
| 720 |
|
| 721 |
|
| 722 |
|
| 723 |
|
| 724 |
|
| 725 |
|
| 726 |
|
| 727 |
|
| 728 |
|
| 729 |
|
| 730 |
|
| 731 |
|
| 732 |
|
| 733 |
|
| 734 |
|
| 735 |
|
| 736 |
|
| 737 |
|
| 738 |
|
| 739 |
|
| 740 |
|
| 741 |
|
| 742 |
|
| 743 |
int |
| 744 |
esl_rnd_Dirichlet(ESL_RANDOMNESS *rng, const double *alpha, int K, double *p) |
| 745 |
{ |
| 746 |
int i; |
| 747 |
double norm = 0.; |
| 748 |
|
| 749 |
for (i = 0; i < K; i++) |
| 750 |
{ |
| 751 |
p[i] = esl_rnd_Gamma(rng, (alpha ? alpha[i] : 1.0)); |
| 752 |
norm += p[i]; |
| 753 |
} |
| 754 |
for (i = 0; i < K; i++) |
| 755 |
p[i] /= norm; |
| 756 |
|
| 757 |
return eslOK; |
| 758 |
} |
| 759 |
|
| 760 |
|
| 761 |
|
| 762 |
|
| 763 |
|
| 764 |
|
| 765 |
|
| 766 |
|
| 767 |
|
| 768 |
|
| 769 |
|
| 770 |
|
| 771 |
|
| 772 |
|
| 773 |
|
| 774 |
|
| 775 |
|
| 776 |
|
| 777 |
|
| 778 |
|
| 779 |
|
| 780 |
|
| 781 |
|
| 782 |
|
| 783 |
|
| 784 |
|
| 785 |
|
| 786 |
|
| 787 |
|
| 788 |
|
| 789 |
int |
| 790 |
esl_rnd_Deal(ESL_RANDOMNESS *rng, int m, int n, int *deal) |
| 791 |
{ |
| 792 |
int i = 0; |
| 793 |
int j = 0; |
| 794 |
|
| 795 |
ESL_DASSERT1(( m >= 0 )); |
| 796 |
ESL_DASSERT1(( n >= 1 )); |
| 797 |
ESL_DASSERT1(( m <= n )); |
| 798 |
|
| 799 |
for (j = 0; j < n && i < m; j++) |
| 800 |
if ( ((double) (n - j)) * esl_random(rng) < (double) (m - i)) deal[i++] = j; |
| 801 |
return eslOK; |
| 802 |
} |
| 803 |
|
| 804 |
|
| 805 |
|
| 806 |
|
| 807 |
|
| 808 |
|
| 809 |
|
| 810 |
|
| 811 |
|
| 812 |
|
| 813 |
|
| 814 |
|
| 815 |
|
| 816 |
|
| 817 |
|
| 818 |
|
| 819 |
|
| 820 |
|
| 821 |
|
| 822 |
|
| 823 |
|
| 824 |
|
| 825 |
|
| 826 |
|
| 827 |
|
| 828 |
|
| 829 |
int |
| 830 |
esl_rnd_DChoose(ESL_RANDOMNESS *r, const double *p, int N) |
| 831 |
{ |
| 832 |
double norm = 0.0; |
| 833 |
double sum = 0.0; |
| 834 |
double roll = esl_random(r); |
| 835 |
int i; |
| 836 |
|
| 837 |
|
| 838 |
for (i = 0; i < N; i++) norm += p[i]; |
| 839 |
ESL_DASSERT1((norm > 0.999 && norm < 1.001)); |
| 840 |
|
| 841 |
for (i = 0; i < N; i++) |
| 842 |
{ |
| 843 |
sum += p[i]; |
| 844 |
if (roll < (sum / norm) ) return i; |
| 845 |
} |
| 846 |
esl_fatal("unreached code was reached. universe collapses."); |
| 847 |
return 0; |
| 848 |
} |
| 849 |
int |
| 850 |
esl_rnd_FChoose(ESL_RANDOMNESS *r, const float *p, int N) |
| 851 |
{ |
| 852 |
|
| 853 |
|
| 854 |
|
| 855 |
double norm = 0.0; |
| 856 |
double sum = 0.0; |
| 857 |
double roll = esl_random(r); |
| 858 |
int i; |
| 859 |
|
| 860 |
for (i = 0; i < N; i++) norm += p[i]; |
| 861 |
ESL_DASSERT1((norm > 0.99 && norm < 1.01)); |
| 862 |
|
| 863 |
for (i = 0; i < N; i++) |
| 864 |
{ |
| 865 |
sum += (double) p[i]; |
| 866 |
if (roll < (sum / norm) ) return i; |
| 867 |
} |
| 868 |
esl_fatal("unreached code was reached. universe collapses."); |
| 869 |
return 0; |
| 870 |
} |
| 871 |
|
| 872 |
|
| 873 |
|
| 874 |
|
| 875 |
|
| 876 |
|
| 877 |
|
| 878 |
|
| 879 |
|
| 880 |
|
| 881 |
|
| 882 |
|
| 883 |
|
| 884 |
|
| 885 |
|
| 886 |
|
| 887 |
|
| 888 |
|
| 889 |
|
| 890 |
|
| 891 |
|
| 892 |
|
| 893 |
|
| 894 |
|
| 895 |
|
| 896 |
|
| 897 |
|
| 898 |
|
| 899 |
|
| 900 |
|
| 901 |
|
| 902 |
|
| 903 |
|
| 904 |
|
| 905 |
|
| 906 |
|
| 907 |
|
| 908 |
|
| 909 |
|
| 910 |
|
| 911 |
int |
| 912 |
esl_rnd_DChooseCDF(ESL_RANDOMNESS *r, const double *cdf, int N) |
| 913 |
{ |
| 914 |
double roll = esl_random(r); |
| 915 |
int i; |
| 916 |
|
| 917 |
ESL_DASSERT1((cdf[0] >= 0.0)); |
| 918 |
ESL_DASSERT1((cdf[N-1] > 0.999 && cdf[N-1] < 1.001)); |
| 919 |
|
| 920 |
for (i = 0; i < N; i++) |
| 921 |
if (roll < cdf[i] / cdf[N-1]) return i; |
| 922 |
esl_fatal("unreached code is reached. universe goes foom"); |
| 923 |
return 0; |
| 924 |
} |
| 925 |
int |
| 926 |
esl_rnd_FChooseCDF(ESL_RANDOMNESS *r, const float *cdf, int N) |
| 927 |
{ |
| 928 |
double roll = esl_random(r); |
| 929 |
int i; |
| 930 |
|
| 931 |
ESL_DASSERT1((cdf[0] >= 0.0)); |
| 932 |
ESL_DASSERT1((cdf[N-1] > 0.99 && cdf[N-1] < 1.01)); |
| 933 |
|
| 934 |
for (i = 0; i < N; i++) |
| 935 |
if (roll < (double) cdf[i] / (double) cdf[N-1]) return i; |
| 936 |
esl_fatal("unreached code is reached. universe goes foom"); |
| 937 |
return 0; |
| 938 |
} |
| 939 |
|
| 940 |
|
| 941 |
|
| 942 |
|
| 943 |
|
| 944 |
|
| 945 |
|
| 946 |
|
| 947 |
|
| 948 |
|
| 949 |
|
| 950 |
|
| 951 |
|
| 952 |
|
| 953 |
|
| 954 |
|
| 955 |
|
| 956 |
|
| 957 |
|
| 958 |
|
| 959 |
int |
| 960 |
esl_rnd_mem(ESL_RANDOMNESS *rng, void *buf, int n) |
| 961 |
{ |
| 962 |
unsigned char *p = (unsigned char *) buf; |
| 963 |
int i; |
| 964 |
|
| 965 |
for (i = 0; i < n; i++) |
| 966 |
p[i] = (unsigned char) esl_rnd_Roll(rng, 256); |
| 967 |
return eslOK; |
| 968 |
} |
| 969 |
|
| 970 |
|
| 971 |
|
| 972 |
|
| 973 |
|
| 974 |
|
| 975 |
|
| 976 |
|
| 977 |
|
| 978 |
|
| 979 |
|
| 980 |
|
| 981 |
|
| 982 |
|
| 983 |
|
| 984 |
|
| 985 |
|
| 986 |
|
| 987 |
|
| 988 |
|
| 989 |
|
| 990 |
|
| 991 |
|
| 992 |
|
| 993 |
|
| 994 |
|
| 995 |
|
| 996 |
|
| 997 |
|
| 998 |
|
| 999 |
|
| 1000 |
|
| 1001 |
|
| 1002 |
|
| 1003 |
|
| 1004 |
|
| 1005 |
|
| 1006 |
|
| 1007 |
|
| 1008 |
|
| 1009 |
int |
| 1010 |
esl_rnd_floatstring(ESL_RANDOMNESS *rng, char *s) |
| 1011 |
{ |
| 1012 |
int i = 0; |
| 1013 |
int n; |
| 1014 |
int exponent; |
| 1015 |
|
| 1016 |
if (esl_rnd_Roll(rng, 2)) s[i++] = '-'; |
| 1017 |
n = esl_rnd_Roll(rng, 7); |
| 1018 |
s[i++] = (n-- == 0) ? '0' : '0' + (1 + esl_rnd_Roll(rng, 9)); |
| 1019 |
while (n-- > 0) s[i++] = '0' + esl_rnd_Roll(rng, 10); |
| 1020 |
if (esl_rnd_Roll(rng, 2)) |
| 1021 |
{ |
| 1022 |
n = 1 + esl_rnd_Roll(rng, 7); |
| 1023 |
s[i++] = '.'; |
| 1024 |
while (n--) s[i++] = '0' + esl_rnd_Roll(rng, 10); |
| 1025 |
} |
| 1026 |
if (esl_rnd_Roll(rng, 2)) |
| 1027 |
{ |
| 1028 |
s[i++] = 'e'; |
| 1029 |
exponent = -20 + esl_rnd_Roll(rng, 41); |
| 1030 |
i += snprintf(s+i, 20-i, "%d", exponent); |
| 1031 |
} |
| 1032 |
s[i++] = '\0'; |
| 1033 |
return eslOK; |
| 1034 |
} |
| 1035 |
|
| 1036 |
|
| 1037 |
|
| 1038 |
|
| 1039 |
|
| 1040 |
|
| 1041 |
|
| 1042 |
#ifdef eslRANDOM_BENCHMARK |
| 1043 |
|
| 1044 |
|
| 1045 |
|
| 1046 |
|
| 1047 |
|
| 1048 |
|
| 1049 |
|
| 1050 |
|
| 1051 |
|
| 1052 |
|
| 1053 |
|
| 1054 |
|
| 1055 |
|
| 1056 |
|
| 1057 |
|
| 1058 |
#include "easel.h" |
| 1059 |
#include "esl_composition.h" |
| 1060 |
#include "esl_getopts.h" |
| 1061 |
#include "esl_random.h" |
| 1062 |
#include "esl_stopwatch.h" |
| 1063 |
#include "esl_vectorops.h" |
| 1064 |
|
| 1065 |
static ESL_OPTIONS options[] = { |
| 1066 |
|
| 1067 |
{ "-h", eslARG_NONE, FALSE, NULL, NULL, NULL, NULL, NULL, "show brief help on version and usage", 0 }, |
| 1068 |
{ "-c", eslARG_NONE, FALSE, NULL, NULL, NULL, NULL, NULL, "benchmark DChooseCDF()", 0 }, |
| 1069 |
{ "-d", eslARG_NONE, FALSE, NULL, NULL, NULL, NULL, NULL, "benchmark DChoose()", 0 }, |
| 1070 |
{ "-f", eslARG_NONE, FALSE, NULL, NULL, NULL, NULL, NULL, "run fast version instead of MT19937", 0 }, |
| 1071 |
{ "-r", eslARG_NONE, FALSE, NULL, NULL, NULL, NULL, NULL, "benchmark _Init(), not just random()", 0 }, |
| 1072 |
{ "-N", eslARG_INT, "10000000",NULL, NULL, NULL, NULL, NULL, "number of trials", 0 }, |
| 1073 |
{ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }, |
| 1074 |
}; |
| 1075 |
static char usage[] = "[-options]"; |
| 1076 |
static char banner[] = "benchmarking speed of random number generator"; |
| 1077 |
|
| 1078 |
int |
| 1079 |
main(int argc, char **argv) |
| 1080 |
{ |
| 1081 |
ESL_GETOPTS *go = esl_getopts_CreateDefaultApp(options, 0, argc, argv, banner, usage); |
| 1082 |
ESL_RANDOMNESS *r = (esl_opt_GetBoolean(go, "-f") == TRUE ? esl_randomness_CreateFast(42) : esl_randomness_Create(42)); |
| 1083 |
ESL_STOPWATCH *w = esl_stopwatch_Create(); |
| 1084 |
int N = esl_opt_GetInteger(go, "-N"); |
| 1085 |
double p[20]; |
| 1086 |
double cdf[20]; |
| 1087 |
|
| 1088 |
esl_composition_BL62(p); |
| 1089 |
esl_vec_DCDF(p, 20, cdf); |
| 1090 |
|
| 1091 |
esl_stopwatch_Start(w); |
| 1092 |
if (esl_opt_GetBoolean(go, "-c")) { while (N--) esl_rnd_DChoose(r, p, 20); } |
| 1093 |
else if (esl_opt_GetBoolean(go, "-d")) { while (N--) esl_rnd_DChooseCDF(r, cdf, 20); } |
| 1094 |
else if (esl_opt_GetBoolean(go, "-r")) { while (N--) esl_randomness_Init(r, 42); } |
| 1095 |
else { while (N--) esl_random(r); } |
| 1096 |
|
| 1097 |
esl_stopwatch_Stop(w); |
| 1098 |
esl_stopwatch_Display(stdout, w, "# CPU Time: "); |
| 1099 |
|
| 1100 |
esl_stopwatch_Destroy(w); |
| 1101 |
esl_randomness_Destroy(r); |
| 1102 |
esl_getopts_Destroy(go); |
| 1103 |
return 0; |
| 1104 |
} |
| 1105 |
#endif |
| 1106 |
|
| 1107 |
|
| 1108 |
|
| 1109 |
|
| 1110 |
|
| 1111 |
|
| 1112 |
|
| 1113 |
|
| 1114 |
|
| 1115 |
#ifdef eslRANDOM_TESTDRIVE |
| 1116 |
#include "esl_vectorops.h" |
| 1117 |
#include "esl_stats.h" |
| 1118 |
#include "esl_dirichlet.h" |
| 1119 |
|
| 1120 |
|
| 1121 |
|
| 1122 |
|
| 1123 |
|
| 1124 |
static void |
| 1125 |
utest_random(ESL_RANDOMNESS *r, int n, int nbins, int be_verbose) |
| 1126 |
{ |
| 1127 |
char msg[] = "esl_random() unit test failed"; |
| 1128 |
int *counts = NULL; |
| 1129 |
double X2p = 0.; |
| 1130 |
int i; |
| 1131 |
int sample; |
| 1132 |
double X2, exp, diff; |
| 1133 |
|
| 1134 |
if ((counts = malloc(sizeof(int) * nbins)) == NULL) esl_fatal(msg); |
| 1135 |
esl_vec_ISet(counts, nbins, 0); |
| 1136 |
|
| 1137 |
for (i = 0; i < n; i++) |
| 1138 |
{ |
| 1139 |
sample = esl_rnd_Roll(r, nbins); |
| 1140 |
if (sample < 0 || sample >= nbins) esl_fatal(msg); |
| 1141 |
counts[sample]++; |
| 1142 |
} |
| 1143 |
|
| 1144 |
|
| 1145 |
for (X2 = 0., i = 0; i < nbins; i++) { |
| 1146 |
exp = (double) n / (double) nbins; |
| 1147 |
diff = (double) counts[i] - exp; |
| 1148 |
X2 += diff*diff/exp; |
| 1149 |
} |
| 1150 |
if (esl_stats_ChiSquaredTest(nbins, X2, &X2p) != eslOK) esl_fatal(msg); |
| 1151 |
if (be_verbose) printf("random(): \t%g\n", X2p); |
| 1152 |
if (X2p < 0.01) esl_fatal(msg); |
| 1153 |
|
| 1154 |
free(counts); |
| 1155 |
return; |
| 1156 |
} |
| 1157 |
|
| 1158 |
|
| 1159 |
|
| 1160 |
static void |
| 1161 |
utest_choose(ESL_RANDOMNESS *r, int n, int nbins, int be_verbose) |
| 1162 |
{ |
| 1163 |
double *pd = NULL; |
| 1164 |
double *pdc = NULL; |
| 1165 |
float *pf = NULL; |
| 1166 |
float *pfc = NULL; |
| 1167 |
int *ct = NULL; |
| 1168 |
int i; |
| 1169 |
double X2, diff, exp, X2p; |
| 1170 |
|
| 1171 |
if ((pd = malloc(sizeof(double) * nbins)) == NULL) esl_fatal("malloc failed"); |
| 1172 |
if ((pdc = malloc(sizeof(double) * nbins)) == NULL) esl_fatal("malloc failed"); |
| 1173 |
if ((pf = malloc(sizeof(float) * nbins)) == NULL) esl_fatal("malloc failed"); |
| 1174 |
if ((pfc = malloc(sizeof(float) * nbins)) == NULL) esl_fatal("malloc failed"); |
| 1175 |
if ((ct = malloc(sizeof(int) * nbins)) == NULL) esl_fatal("malloc failed"); |
| 1176 |
|
| 1177 |
|
| 1178 |
if (esl_dirichlet_DSampleUniform(r, nbins, pd) != eslOK) esl_fatal("dirichlet sample failed"); |
| 1179 |
esl_vec_D2F(pd, nbins, pf); |
| 1180 |
|
| 1181 |
|
| 1182 |
|
| 1183 |
|
| 1184 |
esl_vec_ISet(ct, nbins, 0); |
| 1185 |
for (i = 0; i < n; i++) |
| 1186 |
ct[esl_rnd_DChoose(r, pd, nbins)]++; |
| 1187 |
for (X2 = 0., i=0; i < nbins; i++) { |
| 1188 |
exp = (double) n * pd[i]; |
| 1189 |
diff = (double) ct[i] - exp; |
| 1190 |
X2 += diff*diff/exp; |
| 1191 |
} |
| 1192 |
if (esl_stats_ChiSquaredTest(nbins, X2, &X2p) != eslOK) esl_fatal("chi square eval failed"); |
| 1193 |
if (be_verbose) printf("DChoose(): \t%g\n", X2p); |
| 1194 |
if (X2p < 0.01) esl_fatal("chi squared test failed"); |
| 1195 |
|
| 1196 |
|
| 1197 |
esl_vec_ISet(ct, nbins, 0); |
| 1198 |
for (i = 0; i < n; i++) |
| 1199 |
ct[esl_rnd_FChoose(r, pf, nbins)]++; |
| 1200 |
for (X2 = 0., i=0; i < nbins; i++) { |
| 1201 |
exp = (double) n * pd[i]; |
| 1202 |
diff = (double) ct[i] - exp; |
| 1203 |
X2 += diff*diff/exp; |
| 1204 |
} |
| 1205 |
if (esl_stats_ChiSquaredTest(nbins, X2, &X2p) != eslOK) esl_fatal("chi square eval failed"); |
| 1206 |
if (be_verbose) printf("FChoose(): \t%g\n", X2p); |
| 1207 |
if (X2p < 0.01) esl_fatal("chi squared test failed"); |
| 1208 |
|
| 1209 |
|
| 1210 |
esl_vec_ISet(ct, nbins, 0); |
| 1211 |
esl_vec_DCDF(pd, nbins, pdc); |
| 1212 |
for (i = 0; i < n; i++) |
| 1213 |
ct[esl_rnd_DChooseCDF(r, pdc, nbins)]++; |
| 1214 |
for (X2 = 0., i=0; i < nbins; i++) { |
| 1215 |
exp = (double) n * pd[i]; |
| 1216 |
diff = (double) ct[i] - exp; |
| 1217 |
X2 += diff*diff/exp; |
| 1218 |
} |
| 1219 |
if (esl_stats_ChiSquaredTest(nbins, X2, &X2p) != eslOK) esl_fatal("chi square eval failed"); |
| 1220 |
if (be_verbose) printf("DChoose(): \t%g\n", X2p); |
| 1221 |
if (X2p < 0.01) esl_fatal("chi squared test failed"); |
| 1222 |
|
| 1223 |
|
| 1224 |
esl_vec_ISet(ct, nbins, 0); |
| 1225 |
esl_vec_FCDF(pf, nbins, pfc); |
| 1226 |
for (i = 0; i < n; i++) |
| 1227 |
ct[esl_rnd_FChooseCDF(r, pfc, nbins)]++; |
| 1228 |
for (X2 = 0., i=0; i < nbins; i++) { |
| 1229 |
exp = (double) n * pf[i]; |
| 1230 |
diff = (double) ct[i] - exp; |
| 1231 |
X2 += diff*diff/exp; |
| 1232 |
} |
| 1233 |
if (esl_stats_ChiSquaredTest(nbins, X2, &X2p) != eslOK) esl_fatal("chi square eval failed"); |
| 1234 |
if (be_verbose) printf("DChoose(): \t%g\n", X2p); |
| 1235 |
if (X2p < 0.01) esl_fatal("chi squared test failed"); |
| 1236 |
|
| 1237 |
free(pd); |
| 1238 |
free(pdc); |
| 1239 |
free(pf); |
| 1240 |
free(pfc); |
| 1241 |
free(ct); |
| 1242 |
return; |
| 1243 |
} |
| 1244 |
|
| 1245 |
|
| 1246 |
|
| 1247 |
|
| 1248 |
|
| 1249 |
|
| 1250 |
|
| 1251 |
|
| 1252 |
|
| 1253 |
|
| 1254 |
|
| 1255 |
|
| 1256 |
|
| 1257 |
static void |
| 1258 |
utest_Deal(ESL_RANDOMNESS *rng) |
| 1259 |
{ |
| 1260 |
char msg[] = "esl_random deal unit test failed"; |
| 1261 |
int m = 100; |
| 1262 |
int n = 1000; |
| 1263 |
int nsamp = 10000; |
| 1264 |
int *deal = malloc(sizeof(int) * m); |
| 1265 |
int *ct = malloc(sizeof(int) * n); |
| 1266 |
double expected_mean = ((double) m / (double) n) * (double) nsamp; |
| 1267 |
double expected_sd = sqrt(expected_mean); |
| 1268 |
int max_allowed = (int) round( expected_mean + 6. * expected_sd); |
| 1269 |
int min_allowed = (int) round( expected_mean - 6. * expected_sd); |
| 1270 |
int i; |
| 1271 |
|
| 1272 |
if (deal == NULL || ct == NULL) esl_fatal(msg); |
| 1273 |
esl_vec_ISet(ct, n, 0); |
| 1274 |
|
| 1275 |
while (nsamp--) |
| 1276 |
{ |
| 1277 |
esl_rnd_Deal(rng, m, n, deal); |
| 1278 |
for (i = 0; i < m; i++) ct[deal[i]]++; |
| 1279 |
} |
| 1280 |
if (esl_vec_IMax(ct, n) > max_allowed) esl_fatal(msg); |
| 1281 |
if (esl_vec_IMin(ct, n) < min_allowed) esl_fatal(msg); |
| 1282 |
|
| 1283 |
free(deal); |
| 1284 |
free(ct); |
| 1285 |
} |
| 1286 |
#endif |
| 1287 |
|
| 1288 |
|
| 1289 |
|
| 1290 |
|
| 1291 |
|
| 1292 |
|
| 1293 |
#ifdef eslRANDOM_TESTDRIVE |
| 1294 |
#include <esl_config.h> |
| 1295 |
|
| 1296 |
#include <stdio.h> |
| 1297 |
|
| 1298 |
#include "easel.h" |
| 1299 |
#include "esl_dirichlet.h" |
| 1300 |
#include "esl_getopts.h" |
| 1301 |
#include "esl_random.h" |
| 1302 |
#include "esl_randomseq.h" |
| 1303 |
#include "esl_vectorops.h" |
| 1304 |
|
| 1305 |
static ESL_OPTIONS options[] = { |
| 1306 |
|
| 1307 |
{"-h", eslARG_NONE, FALSE, NULL, NULL, NULL, NULL, NULL, "show help and usage", 0}, |
| 1308 |
{"-b", eslARG_INT, "20", NULL, "n>0",NULL, NULL, NULL, "number of test bins", 0}, |
| 1309 |
{"-n", eslARG_INT, "1000000", NULL, "n>0",NULL, NULL, NULL, "number of samples", 0}, |
| 1310 |
{"-s", eslARG_INT, "42", NULL, NULL, NULL, NULL, NULL, "set random number seed to <n>", 0}, |
| 1311 |
{"-v", eslARG_NONE, FALSE, NULL, NULL, NULL, NULL, NULL, "show verbose output", 0}, |
| 1312 |
{"--mtbits",eslARG_STRING,NULL,NULL, NULL, NULL, NULL, NULL, "save MT bit file for NIST benchmark",0}, |
| 1313 |
{"--kbits", eslARG_STRING,NULL,NULL, NULL, NULL, NULL, NULL, "save Knuth bit file for NIST benchmark",0}, |
| 1314 |
{ 0,0,0,0,0,0,0,0,0,0}, |
| 1315 |
}; |
| 1316 |
static char usage[] = "[-options]"; |
| 1317 |
static char banner[] = "test driver for random module"; |
| 1318 |
|
| 1319 |
static int save_bitfile(char *bitfile, ESL_RANDOMNESS *r, int n); |
| 1320 |
|
| 1321 |
int |
| 1322 |
main(int argc, char **argv) |
| 1323 |
{ |
| 1324 |
ESL_GETOPTS *go = esl_getopts_CreateDefaultApp(options, 0, argc, argv, banner, usage); |
| 1325 |
ESL_RANDOMNESS *r1 = esl_randomness_Create(esl_opt_GetInteger(go, "-s")); |
| 1326 |
ESL_RANDOMNESS *r2 = esl_randomness_CreateFast(esl_opt_GetInteger(go, "-s")); |
| 1327 |
char *mtbitfile = esl_opt_GetString (go, "--mtbits"); |
| 1328 |
char *kbitfile = esl_opt_GetString (go, "--kbits"); |
| 1329 |
int nbins = esl_opt_GetInteger(go, "-b"); |
| 1330 |
int n = esl_opt_GetInteger(go, "-n"); |
| 1331 |
int be_verbose = esl_opt_GetBoolean(go, "-v"); |
| 1332 |
|
| 1333 |
fprintf(stderr, "## %s\n", argv[0]); |
| 1334 |
fprintf(stderr, "# rng seed 1 (slow) = %" PRIu32 "\n", esl_randomness_GetSeed(r1)); |
| 1335 |
fprintf(stderr, "# rng seed 2 (fast) = %" PRIu32 "\n", esl_randomness_GetSeed(r2)); |
| 1336 |
|
| 1337 |
utest_random(r1, n, nbins, be_verbose); |
| 1338 |
utest_choose(r1, n, nbins, be_verbose); |
| 1339 |
utest_random(r2, n, nbins, be_verbose); |
| 1340 |
utest_choose(r2, n, nbins, be_verbose); |
| 1341 |
|
| 1342 |
utest_Deal(r1); |
| 1343 |
|
| 1344 |
if (mtbitfile) save_bitfile(mtbitfile, r1, n); |
| 1345 |
if (kbitfile) save_bitfile(kbitfile, r2, n); |
| 1346 |
|
| 1347 |
fprintf(stderr, "# status = ok\n"); |
| 1348 |
|
| 1349 |
esl_randomness_Destroy(r1); |
| 1350 |
esl_randomness_Destroy(r2); |
| 1351 |
esl_getopts_Destroy(go); |
| 1352 |
return 0; |
| 1353 |
} |
| 1354 |
|
| 1355 |
static int |
| 1356 |
save_bitfile(char *bitfile, ESL_RANDOMNESS *r, int n) |
| 1357 |
{ |
| 1358 |
FILE *fp = NULL; |
| 1359 |
int b,i; |
| 1360 |
long x; |
| 1361 |
|
| 1362 |
|
| 1363 |
|
| 1364 |
if ((fp = fopen(bitfile, "w")) == NULL) |
| 1365 |
esl_fatal("failed to open %s for writing", bitfile); |
| 1366 |
|
| 1367 |
|
| 1368 |
|
| 1369 |
for (i = 0; i < n; i++) |
| 1370 |
{ |
| 1371 |
x = (r->type == eslRND_FAST ? knuth(r) : mersenne_twister(r)); |
| 1372 |
for (b = 0; b < 32; b++) |
| 1373 |
{ |
| 1374 |
if (x & 01) fprintf(fp, "1"); |
| 1375 |
else fprintf(fp, "0"); |
| 1376 |
x >>= 1; |
| 1377 |
} |
| 1378 |
fprintf(fp, "\n"); |
| 1379 |
} |
| 1380 |
fclose(fp); |
| 1381 |
return eslOK; |
| 1382 |
} |
| 1383 |
#endif |
| 1384 |
|
| 1385 |
|
| 1386 |
|
| 1387 |
|
| 1388 |
|
| 1389 |
|
| 1390 |
#ifdef eslRANDOM_EXAMPLE |
| 1391 |
|
| 1392 |
|
| 1393 |
|
| 1394 |
|
| 1395 |
#include <stdio.h> |
| 1396 |
#include "easel.h" |
| 1397 |
#include "esl_getopts.h" |
| 1398 |
#include "esl_random.h" |
| 1399 |
|
| 1400 |
|
| 1401 |
|
| 1402 |
|
| 1403 |
|
| 1404 |
|
| 1405 |
|
| 1406 |
|
| 1407 |
|
| 1408 |
static ESL_OPTIONS options[] = { |
| 1409 |
|
| 1410 |
{ "-h", eslARG_NONE, FALSE, NULL, NULL, NULL, NULL, NULL, "show brief help on version and usage", 0 }, |
| 1411 |
{ "-i", eslARG_NONE, FALSE, NULL, NULL, NULL, NULL, NULL, "sample uint32's instead of doubles", 0 }, |
| 1412 |
{ "-n", eslARG_INT, "20", NULL, NULL, NULL, NULL, NULL, "number of random samples to show", 0 }, |
| 1413 |
{ "-s", eslARG_INT, "0", NULL, NULL, NULL, NULL, NULL, "set random number seed to <n>", 0 }, |
| 1414 |
{ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }, |
| 1415 |
}; |
| 1416 |
static char usage[] = "[-options]"; |
| 1417 |
static char banner[] = "example of using random module"; |
| 1418 |
|
| 1419 |
int |
| 1420 |
main(int argc, char **argv) |
| 1421 |
{ |
| 1422 |
ESL_GETOPTS *go = esl_getopts_CreateDefaultApp(options, 0, argc, argv, banner, usage); |
| 1423 |
ESL_RANDOMNESS *rng = esl_randomness_Create( esl_opt_GetInteger(go, "-s") ); |
| 1424 |
int do_uint32 = esl_opt_GetBoolean(go, "-i"); |
| 1425 |
int n = esl_opt_GetInteger(go, "-n"); |
| 1426 |
|
| 1427 |
printf("RNG seed: %" PRIu32 "\n", esl_randomness_GetSeed(rng)); |
| 1428 |
printf("\nA sequence of %d pseudorandom numbers:\n", n); |
| 1429 |
if (do_uint32) while (n--) printf("%" PRIu32 "\n", esl_random_uint32(rng)); |
| 1430 |
else while (n--) printf("%f\n", esl_random(rng)); |
| 1431 |
|
| 1432 |
printf("\nInternal dump of RNG state:\n"); |
| 1433 |
esl_randomness_Dump(stdout, rng); |
| 1434 |
|
| 1435 |
esl_randomness_Destroy(rng); |
| 1436 |
esl_getopts_Destroy(go); |
| 1437 |
return 0; |
| 1438 |
} |
| 1439 |
|
| 1440 |
#endif |
| 1441 |
|
| 1442 |
|
| 1443 |
|