| 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 |
|
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
#include <esl_config.h> |
| 32 |
|
| 33 |
#include <stdlib.h> |
| 34 |
#include <stdint.h> |
| 35 |
#include <stdio.h> |
| 36 |
#include <time.h> |
| 37 |
#ifdef HAVE_UNISTD_H |
| 38 |
#include <unistd.h> |
| 39 |
#endif |
| 40 |
|
| 41 |
#include "easel.h" |
| 42 |
#include "esl_rand64.h" |
| 43 |
|
| 44 |
|
| 45 |
|
| 46 |
#if !defined(eslRAND64_BENCHMARK) && !defined(eslRAND64_TESTDRIVE) && !defined(eslRAND64_EXAMPLE) |
| 47 |
|
| 48 |
|
| 49 |
|
| 50 |
|
| 51 |
|
| 52 |
|
| 53 |
|
| 54 |
|
| 55 |
|
| 56 |
|
| 57 |
|
| 58 |
static void mt64_seed_table(ESL_RAND64 *rng, uint64_t seed); |
| 59 |
static void mt64_fill_table(ESL_RAND64 *rng); |
| 60 |
static uint64_t choose_arbitrary_seed(void); |
| 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 |
|
| 90 |
|
| 91 |
|
| 92 |
ESL_RAND64 * |
| 93 |
esl_rand64_Create(uint64_t seed) |
| 94 |
{ |
| 95 |
ESL_RAND64 *rng = NULL; |
| 96 |
int status; |
| 97 |
|
| 98 |
ESL_ALLOC(rng, sizeof(ESL_RAND64)); |
| 99 |
rng->mti = 0; |
| 100 |
rng->seed = 0; |
| 101 |
esl_rand64_Init(rng, seed); |
| 102 |
return rng; |
| 103 |
|
| 104 |
ERROR: |
| 105 |
return NULL; |
| 106 |
} |
| 107 |
|
| 108 |
|
| 109 |
|
| 110 |
|
| 111 |
|
| 112 |
|
| 113 |
|
| 114 |
|
| 115 |
|
| 116 |
|
| 117 |
|
| 118 |
|
| 119 |
|
| 120 |
|
| 121 |
|
| 122 |
|
| 123 |
|
| 124 |
|
| 125 |
|
| 126 |
int |
| 127 |
esl_rand64_Init(ESL_RAND64 *rng, uint64_t seed) |
| 128 |
{ |
| 129 |
if (seed == 0ULL) seed = choose_arbitrary_seed(); |
| 130 |
mt64_seed_table(rng, seed); |
| 131 |
mt64_fill_table(rng); |
| 132 |
return eslOK; |
| 133 |
} |
| 134 |
|
| 135 |
|
| 136 |
|
| 137 |
|
| 138 |
|
| 139 |
|
| 140 |
|
| 141 |
|
| 142 |
|
| 143 |
|
| 144 |
|
| 145 |
|
| 146 |
uint64_t |
| 147 |
esl_rand64_GetSeed(ESL_RAND64 *rng) |
| 148 |
{ |
| 149 |
return rng->seed; |
| 150 |
} |
| 151 |
|
| 152 |
|
| 153 |
|
| 154 |
|
| 155 |
|
| 156 |
|
| 157 |
void |
| 158 |
esl_rand64_Destroy(ESL_RAND64 *rng) |
| 159 |
{ |
| 160 |
free(rng); |
| 161 |
} |
| 162 |
|
| 163 |
|
| 164 |
|
| 165 |
|
| 166 |
|
| 167 |
|
| 168 |
|
| 169 |
|
| 170 |
|
| 171 |
|
| 172 |
|
| 173 |
|
| 174 |
uint64_t |
| 175 |
esl_rand64(ESL_RAND64 *rng) |
| 176 |
{ |
| 177 |
uint64_t x; |
| 178 |
if (rng->mti >= 312) mt64_fill_table(rng); |
| 179 |
|
| 180 |
x = rng->mt[rng->mti++]; |
| 181 |
x ^= (x >> 29) & 0x5555555555555555ULL; |
| 182 |
x ^= (x << 17) & 0x71D67FFFEDA60000ULL; |
| 183 |
x ^= (x << 37) & 0xFFF7EEE000000000ULL; |
| 184 |
x ^= (x >> 43); |
| 185 |
return x; |
| 186 |
} |
| 187 |
|
| 188 |
|
| 189 |
|
| 190 |
|
| 191 |
|
| 192 |
|
| 193 |
|
| 194 |
|
| 195 |
|
| 196 |
|
| 197 |
|
| 198 |
|
| 199 |
|
| 200 |
|
| 201 |
|
| 202 |
|
| 203 |
int64_t |
| 204 |
esl_rand64_int64(ESL_RAND64 *rng) |
| 205 |
{ |
| 206 |
return ((int64_t) (esl_rand64(rng) >> 1)); |
| 207 |
} |
| 208 |
|
| 209 |
|
| 210 |
|
| 211 |
|
| 212 |
|
| 213 |
|
| 214 |
uint64_t |
| 215 |
esl_rand64_Roll(ESL_RAND64 *rng, uint64_t n) |
| 216 |
{ |
| 217 |
uint64_t factor = UINT64_MAX / n; |
| 218 |
uint64_t x; |
| 219 |
do { x = esl_rand64(rng) / factor; } while (x >= n); |
| 220 |
return x; |
| 221 |
} |
| 222 |
|
| 223 |
|
| 224 |
|
| 225 |
|
| 226 |
|
| 227 |
|
| 228 |
|
| 229 |
|
| 230 |
double |
| 231 |
esl_rand64_double(ESL_RAND64 *rng) |
| 232 |
{ |
| 233 |
return (double) (esl_rand64(rng) >> 11) * (1.0/9007199254740992.0); |
| 234 |
} |
| 235 |
|
| 236 |
|
| 237 |
|
| 238 |
|
| 239 |
|
| 240 |
|
| 241 |
|
| 242 |
|
| 243 |
|
| 244 |
double |
| 245 |
esl_rand64_double_closed(ESL_RAND64 *rng) |
| 246 |
{ |
| 247 |
return ((double) (esl_rand64(rng) >> 11)) * (1.0/9007199254740991.0); |
| 248 |
} |
| 249 |
|
| 250 |
|
| 251 |
|
| 252 |
|
| 253 |
|
| 254 |
|
| 255 |
|
| 256 |
|
| 257 |
|
| 258 |
double |
| 259 |
esl_rand64_double_open(ESL_RAND64 *rng) |
| 260 |
{ |
| 261 |
return ((double) (esl_rand64(rng) >> 12) + 0.5) * (1.0/4503599627370496.0); |
| 262 |
} |
| 263 |
|
| 264 |
|
| 265 |
|
| 266 |
|
| 267 |
|
| 268 |
|
| 269 |
|
| 270 |
|
| 271 |
|
| 272 |
|
| 273 |
|
| 274 |
|
| 275 |
|
| 276 |
|
| 277 |
|
| 278 |
|
| 279 |
|
| 280 |
|
| 281 |
|
| 282 |
|
| 283 |
|
| 284 |
|
| 285 |
|
| 286 |
|
| 287 |
|
| 288 |
|
| 289 |
static int |
| 290 |
vitter_a(ESL_RAND64 *rng, int64_t m, int64_t n, int64_t j, int64_t *deal) |
| 291 |
{ |
| 292 |
int64_t i = 0; |
| 293 |
int64_t S = 0; |
| 294 |
double top = (double) (n - m); |
| 295 |
double nreal = (double) n; |
| 296 |
double U, quot; |
| 297 |
|
| 298 |
while (m >= 2) |
| 299 |
{ |
| 300 |
U = esl_rand64_double_open(rng); |
| 301 |
S = 0; |
| 302 |
quot = top/nreal; |
| 303 |
while (quot > U) |
| 304 |
{ |
| 305 |
S++; |
| 306 |
top -= 1.; |
| 307 |
nreal -= 1.; |
| 308 |
quot = (quot * top)/nreal; |
| 309 |
} |
| 310 |
j += S+1; |
| 311 |
deal[i++] = j; |
| 312 |
nreal -= 1; |
| 313 |
m--; |
| 314 |
} |
| 315 |
S = floor(round(nreal) * esl_rand64_double(rng)); |
| 316 |
j += S+1; |
| 317 |
deal[i++] = j; |
| 318 |
return eslOK; |
| 319 |
} |
| 320 |
|
| 321 |
|
| 322 |
|
| 323 |
|
| 324 |
|
| 325 |
|
| 326 |
|
| 327 |
|
| 328 |
|
| 329 |
|
| 330 |
|
| 331 |
|
| 332 |
|
| 333 |
|
| 334 |
|
| 335 |
|
| 336 |
|
| 337 |
|
| 338 |
|
| 339 |
|
| 340 |
|
| 341 |
|
| 342 |
|
| 343 |
|
| 344 |
|
| 345 |
|
| 346 |
|
| 347 |
|
| 348 |
|
| 349 |
|
| 350 |
|
| 351 |
|
| 352 |
|
| 353 |
|
| 354 |
|
| 355 |
|
| 356 |
|
| 357 |
|
| 358 |
|
| 359 |
int |
| 360 |
esl_rand64_Deal(ESL_RAND64 *rng, int64_t m, int64_t n, int64_t *deal) |
| 361 |
{ |
| 362 |
int64_t i = 0; |
| 363 |
int64_t j = -1; |
| 364 |
int64_t S; |
| 365 |
int64_t qu1 = n - m + 1; |
| 366 |
int64_t negalphainv = -13; |
| 367 |
int64_t threshold = -negalphainv*m; |
| 368 |
double mreal = (double) m; |
| 369 |
double nreal = (double) n; |
| 370 |
double minv = 1.0 / (double) m; |
| 371 |
double mmin1inv = 1.0 / (double) (m-1); |
| 372 |
double Vprime = exp(minv * log(esl_rand64_double(rng))); |
| 373 |
double qu1real = nreal - mreal + 1.0; |
| 374 |
int64_t t, limit; |
| 375 |
double negSreal, U, X, y1, y2, top, bottom; |
| 376 |
|
| 377 |
ESL_DASSERT1(( m <= n )); |
| 378 |
|
| 379 |
while (m > 1 && n > threshold) |
| 380 |
{ |
| 381 |
mmin1inv = 1.0 / (-1.0 + mreal); |
| 382 |
while (1) |
| 383 |
{ |
| 384 |
while (1) |
| 385 |
{ |
| 386 |
X = nreal * (-Vprime + 1.0); |
| 387 |
if ((S = floor(X)) < qu1) break; |
| 388 |
Vprime = exp(minv * log(esl_rand64_double_open(rng))); |
| 389 |
} |
| 390 |
U = esl_rand64_double_open(rng); |
| 391 |
negSreal = -S; |
| 392 |
y1 = exp(mmin1inv * log(U*nreal/qu1real)); |
| 393 |
Vprime = y1 * (-X/nreal + 1.0) * (qu1real / (negSreal + qu1real)); |
| 394 |
if (Vprime <= 1.) break; |
| 395 |
|
| 396 |
y2 = 1.; |
| 397 |
top = nreal - 1.; |
| 398 |
|
| 399 |
if (n-1 > S) { bottom = nreal - mreal; limit = n - S; } |
| 400 |
else { bottom = nreal + negSreal - 1.; limit = qu1; } |
| 401 |
|
| 402 |
for (t = n-1; t >= limit; t--) |
| 403 |
{ y2 = (y2 * top) / bottom; top--; bottom--; } |
| 404 |
|
| 405 |
if (nreal / (nreal-X) >= y1 * exp(mmin1inv * log(y2))) |
| 406 |
{ Vprime = exp(mmin1inv * log(esl_rand64_double_open(rng))); break; } |
| 407 |
|
| 408 |
Vprime = exp(minv * log(esl_rand64_double_open(rng))); |
| 409 |
} |
| 410 |
j += S+1; |
| 411 |
deal[i++] = j; |
| 412 |
n = n - S - 1; |
| 413 |
nreal = nreal + negSreal - 1.; |
| 414 |
m -= 1; |
| 415 |
mreal -= 1.0; |
| 416 |
minv = mmin1inv; |
| 417 |
qu1 = qu1 - S; |
| 418 |
qu1real = qu1real + negSreal; |
| 419 |
threshold += negalphainv; |
| 420 |
} |
| 421 |
|
| 422 |
if (m > 1) vitter_a(rng, m, n, j, deal+i); |
| 423 |
else { |
| 424 |
S = floor(n * Vprime); |
| 425 |
j += S+1; |
| 426 |
deal[i++] = j; |
| 427 |
} |
| 428 |
return eslOK; |
| 429 |
} |
| 430 |
|
| 431 |
|
| 432 |
|
| 433 |
|
| 434 |
|
| 435 |
|
| 436 |
|
| 437 |
|
| 438 |
|
| 439 |
|
| 440 |
|
| 441 |
|
| 442 |
static void |
| 443 |
mt64_seed_table(ESL_RAND64 *rng, uint64_t seed) |
| 444 |
{ |
| 445 |
int z; |
| 446 |
rng->mt[0] = rng->seed = seed; |
| 447 |
for (z = 1; z < 312; z++) |
| 448 |
rng->mt[z] = 6364136223846793005ULL * (rng->mt[z-1] ^ (rng->mt[z-1] >> 62)) + z; |
| 449 |
} |
| 450 |
|
| 451 |
|
| 452 |
|
| 453 |
|
| 454 |
|
| 455 |
static void |
| 456 |
mt64_fill_table(ESL_RAND64 *rng) |
| 457 |
{ |
| 458 |
static uint64_t mag01[2] = { 0ULL, 0xB5026F5AA96619E9ULL }; |
| 459 |
uint64_t x; |
| 460 |
int z; |
| 461 |
|
| 462 |
for (z = 0; z < 156; z++) |
| 463 |
{ |
| 464 |
x = (rng->mt[z] & 0xFFFFFFFF80000000ULL) | (rng->mt[z+1] & 0x7FFFFFFFULL); |
| 465 |
rng->mt[z] = rng->mt[z+156] ^ (x>>1) ^ mag01[(int)(x & 1ULL)]; |
| 466 |
} |
| 467 |
for (; z < 311; z++) |
| 468 |
{ |
| 469 |
x = (rng->mt[z] & 0xFFFFFFFF80000000ULL) | (rng->mt[z+1] & 0x7FFFFFFFULL); |
| 470 |
rng->mt[z] = rng->mt[z-156] ^ (x>>1) ^ mag01[(int)(x & 1ULL)]; |
| 471 |
} |
| 472 |
x = (rng->mt[311] & 0xFFFFFFFF80000000ULL) | (rng->mt[0] & 0x7FFFFFFFULL); |
| 473 |
rng->mt[311] = rng->mt[155] ^ (x>>1) ^ mag01[(int)(x & 1ULL)]; |
| 474 |
rng->mti = 0; |
| 475 |
} |
| 476 |
|
| 477 |
|
| 478 |
|
| 479 |
|
| 480 |
|
| 481 |
|
| 482 |
|
| 483 |
|
| 484 |
|
| 485 |
|
| 486 |
|
| 487 |
|
| 488 |
|
| 489 |
|
| 490 |
|
| 491 |
|
| 492 |
static uint64_t |
| 493 |
choose_arbitrary_seed(void) |
| 494 |
{ |
| 495 |
uint32_t a = (uint32_t) time ((time_t *) NULL); |
| 496 |
uint32_t b = 87654321; |
| 497 |
uint32_t c = (uint32_t) clock(); |
| 498 |
uint64_t seed; |
| 499 |
#ifdef HAVE_GETPID |
| 500 |
b = (uint32_t) getpid(); |
| 501 |
#endif |
| 502 |
seed = (uint64_t) esl_mix3(a,b,c); |
| 503 |
seed = (seed << 32) + (uint64_t) esl_mix3(c,a,b); |
| 504 |
return (seed == 0 ? 42 : seed); |
| 505 |
} |
| 506 |
|
| 507 |
|
| 508 |
|
| 509 |
|
| 510 |
|
| 511 |
|
| 512 |
|
| 513 |
|
| 514 |
|
| 515 |
|
| 516 |
int |
| 517 |
esl_rand64_Dump(FILE *fp, ESL_RAND64 *rng) |
| 518 |
{ |
| 519 |
int i; |
| 520 |
|
| 521 |
fprintf(fp, "MT19937-64 RNG state:\n"); |
| 522 |
fprintf(fp, "mti = %d (0..311)\n", rng->mti); |
| 523 |
fprintf(fp, "seed = %" PRIu64 "\n", rng->seed); |
| 524 |
for (i = 0; i < 312; i++) |
| 525 |
{ |
| 526 |
fprintf(fp, "%20" PRIu64 " ", rng->mt[i]); |
| 527 |
if (i % 10 == 9) fprintf(fp, "\n"); |
| 528 |
} |
| 529 |
fprintf(fp, "\n"); |
| 530 |
return eslOK; |
| 531 |
} |
| 532 |
|
| 533 |
#endif |
| 534 |
|
| 535 |
|
| 536 |
|
| 537 |
|
| 538 |
|
| 539 |
#ifdef eslRAND64_BENCHMARK |
| 540 |
|
| 541 |
#include "easel.h" |
| 542 |
#include "esl_getopts.h" |
| 543 |
#include "esl_rand64.h" |
| 544 |
#include "esl_stopwatch.h" |
| 545 |
|
| 546 |
|
| 547 |
|
| 548 |
|
| 549 |
|
| 550 |
|
| 551 |
|
| 552 |
|
| 553 |
|
| 554 |
|
| 555 |
static ESL_OPTIONS options[] = { |
| 556 |
|
| 557 |
{ "-h", eslARG_NONE, FALSE, NULL, NULL, NULL, NULL, NULL, "show brief help on version and usage", 0 }, |
| 558 |
{ "-N", eslARG_INT, "1000000000", NULL, NULL, NULL, NULL, NULL, "number of trials", 0 }, |
| 559 |
{ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }, |
| 560 |
}; |
| 561 |
static char usage[] = "[-options]"; |
| 562 |
static char banner[] = "benchmarking speed of esl_rand64 generator"; |
| 563 |
|
| 564 |
int |
| 565 |
main(int argc, char **argv) |
| 566 |
{ |
| 567 |
ESL_GETOPTS *go = esl_getopts_CreateDefaultApp(options, 0, argc, argv, banner, usage); |
| 568 |
ESL_RAND64 *rng = esl_rand64_Create(42); |
| 569 |
ESL_STOPWATCH *w = esl_stopwatch_Create(); |
| 570 |
int N = esl_opt_GetInteger(go, "-N"); |
| 571 |
|
| 572 |
esl_stopwatch_Start(w); |
| 573 |
while (N--) esl_rand64(rng); |
| 574 |
esl_stopwatch_Stop(w); |
| 575 |
esl_stopwatch_Display(stdout, w, "# CPU Time: "); |
| 576 |
|
| 577 |
esl_stopwatch_Destroy(w); |
| 578 |
esl_rand64_Destroy(rng); |
| 579 |
esl_getopts_Destroy(go); |
| 580 |
return 0; |
| 581 |
} |
| 582 |
#endif |
| 583 |
|
| 584 |
|
| 585 |
|
| 586 |
|
| 587 |
|
| 588 |
|
| 589 |
#ifdef eslRAND64_TESTDRIVE |
| 590 |
|
| 591 |
#include "esl_stats.h" |
| 592 |
#include "esl_vectorops.h" |
| 593 |
|
| 594 |
|
| 595 |
|
| 596 |
enum esl_rand64_utest_e { |
| 597 |
eslRAND64_LOWBITS, |
| 598 |
eslRAND64_HIGHBITS, |
| 599 |
eslRAND64_ROLL, |
| 600 |
eslRAND64_DOUBLE, |
| 601 |
eslRAND64_DOUBLE_CLOSED, |
| 602 |
eslRAND64_DOUBLE_OPEN, |
| 603 |
}; |
| 604 |
|
| 605 |
|
| 606 |
|
| 607 |
|
| 608 |
|
| 609 |
|
| 610 |
|
| 611 |
static void |
| 612 |
utest_rand64(ESL_RAND64 *rng, enum esl_rand64_utest_e whichtest) |
| 613 |
{ |
| 614 |
char msg[] = "rand64 unit test failed"; |
| 615 |
int n = 2000000; |
| 616 |
int nbins = 1024; |
| 617 |
uint64_t divisor = (UINT64_MAX / nbins) + 1; |
| 618 |
int *counts = NULL; |
| 619 |
uint64_t x; |
| 620 |
double xd; |
| 621 |
int b; |
| 622 |
double expect, diff, X2, X2p; |
| 623 |
int i; |
| 624 |
|
| 625 |
if (( counts = malloc(sizeof(int) * nbins) ) == NULL) esl_fatal(msg); |
| 626 |
esl_vec_ISet(counts, nbins, 0); |
| 627 |
|
| 628 |
if (whichtest == eslRAND64_LOWBITS || whichtest == eslRAND64_HIGHBITS) |
| 629 |
{ |
| 630 |
for (i = 0; i < n; i++) |
| 631 |
{ |
| 632 |
x = esl_rand64(rng); |
| 633 |
if (whichtest == eslRAND64_HIGHBITS) b = (int) (x / divisor); |
| 634 |
else b = (int) (x % nbins); |
| 635 |
counts[b]++; |
| 636 |
} |
| 637 |
} |
| 638 |
else if (whichtest == eslRAND64_ROLL) |
| 639 |
{ |
| 640 |
for (i = 0; i < n; i++) |
| 641 |
{ |
| 642 |
b = esl_rand64_Roll(rng, nbins); |
| 643 |
if (b < 0 || b >= nbins) esl_fatal(msg); |
| 644 |
counts[b]++; |
| 645 |
} |
| 646 |
} |
| 647 |
else |
| 648 |
{ |
| 649 |
for (i = 0; i < n; i++) |
| 650 |
{ |
| 651 |
if (whichtest == eslRAND64_DOUBLE) { xd = esl_rand64_double(rng); if (xd < 0.0 || xd >= 1.0) esl_fatal(msg); } |
| 652 |
else if (whichtest == eslRAND64_DOUBLE_CLOSED) { xd = esl_rand64_double_closed(rng); if (xd < 0.0 || xd > 1.0) esl_fatal(msg); } |
| 653 |
else if (whichtest == eslRAND64_DOUBLE_OPEN) { xd = esl_rand64_double_open(rng); if (xd <= 0.0 || xd >= 1.0) esl_fatal(msg); } |
| 654 |
b = (int) (xd * nbins); |
| 655 |
if (b < 0 || b >= nbins) esl_fatal(msg); |
| 656 |
counts[b] ++; |
| 657 |
} |
| 658 |
} |
| 659 |
|
| 660 |
|
| 661 |
expect = (double) n / (double) nbins; |
| 662 |
X2 = 0.; |
| 663 |
for (i = 0; i < nbins; i++) |
| 664 |
{ |
| 665 |
diff = (double) counts[i] - expect; |
| 666 |
X2 += diff * diff / expect; |
| 667 |
} |
| 668 |
|
| 669 |
if ( esl_stats_ChiSquaredTest(nbins, X2, &X2p) != eslOK) esl_fatal(msg); |
| 670 |
if (X2p < 0.01) esl_fatal(msg); |
| 671 |
free(counts); |
| 672 |
} |
| 673 |
|
| 674 |
|
| 675 |
|
| 676 |
|
| 677 |
|
| 678 |
|
| 679 |
|
| 680 |
|
| 681 |
|
| 682 |
|
| 683 |
|
| 684 |
|
| 685 |
static void |
| 686 |
utest_Deal(ESL_RAND64 *rng) |
| 687 |
{ |
| 688 |
char msg[] = "esl_rand64 deal unit test failed"; |
| 689 |
int64_t m = 100; |
| 690 |
int64_t n = 1000; |
| 691 |
int64_t nsamp = 10000; |
| 692 |
int64_t *deal = malloc(sizeof(int64_t) * m); |
| 693 |
int64_t *ct = malloc(sizeof(int64_t) * n); |
| 694 |
double expected_mean = ((double) m / (double) n) * (double) nsamp; |
| 695 |
double expected_sd = sqrt(expected_mean); |
| 696 |
int64_t max_allowed = (int64_t) round( expected_mean + 6. * expected_sd); |
| 697 |
int64_t min_allowed = (int64_t) round( expected_mean - 6. * expected_sd); |
| 698 |
int64_t i; |
| 699 |
|
| 700 |
if (deal == NULL || ct == NULL) esl_fatal(msg); |
| 701 |
esl_vec_LSet(ct, n, 0); |
| 702 |
|
| 703 |
while (nsamp--) |
| 704 |
{ |
| 705 |
esl_rand64_Deal(rng, m, n, deal); |
| 706 |
for (i = 0; i < m; i++) ct[deal[i]]++; |
| 707 |
} |
| 708 |
if (esl_vec_LMax(ct, n) > max_allowed) esl_fatal(msg); |
| 709 |
if (esl_vec_LMin(ct, n) < min_allowed) esl_fatal(msg); |
| 710 |
|
| 711 |
free(deal); |
| 712 |
free(ct); |
| 713 |
} |
| 714 |
#endif |
| 715 |
|
| 716 |
|
| 717 |
|
| 718 |
|
| 719 |
|
| 720 |
|
| 721 |
#ifdef eslRAND64_TESTDRIVE |
| 722 |
|
| 723 |
#include "easel.h" |
| 724 |
#include "esl_getopts.h" |
| 725 |
#include "esl_rand64.h" |
| 726 |
|
| 727 |
|
| 728 |
static ESL_OPTIONS options[] = { |
| 729 |
|
| 730 |
{ "-h", eslARG_NONE, FALSE, NULL, NULL, NULL, NULL, NULL, "show help and usage", 0}, |
| 731 |
{ "--bitfile", eslARG_STRING, NULL, NULL, NULL, NULL, NULL, NULL, "save bit file for NIST tests", 0}, |
| 732 |
{ "--seed", eslARG_INT, "42", NULL, NULL, NULL, NULL, NULL, "set random number seed to <n>", 0}, |
| 733 |
{ 0,0,0,0,0,0,0,0,0,0}, |
| 734 |
}; |
| 735 |
static char usage[] = "[-options]"; |
| 736 |
static char banner[] = "test driver for rand64 module"; |
| 737 |
|
| 738 |
static int save_bitfile(char *bitfile, ESL_RAND64 *rng); |
| 739 |
|
| 740 |
int |
| 741 |
main(int argc, char **argv) |
| 742 |
{ |
| 743 |
ESL_GETOPTS *go = esl_getopts_CreateDefaultApp(options, 0, argc, argv, banner, usage); |
| 744 |
ESL_RAND64 *rng = esl_rand64_Create(esl_opt_GetInteger(go, "--seed")); |
| 745 |
char *bitfile = esl_opt_GetString(go, "--bitfile"); |
| 746 |
|
| 747 |
if (bitfile) |
| 748 |
{ |
| 749 |
save_bitfile(bitfile, rng); |
| 750 |
} |
| 751 |
else |
| 752 |
{ |
| 753 |
fprintf(stderr, "## %s\n", argv[0]); |
| 754 |
fprintf(stderr, "# rng seed = %" PRIu64 "\n", esl_rand64_GetSeed(rng)); |
| 755 |
|
| 756 |
utest_rand64(rng, eslRAND64_LOWBITS); |
| 757 |
utest_rand64(rng, eslRAND64_HIGHBITS); |
| 758 |
utest_rand64(rng, eslRAND64_ROLL); |
| 759 |
utest_rand64(rng, eslRAND64_DOUBLE); |
| 760 |
utest_rand64(rng, eslRAND64_DOUBLE_CLOSED); |
| 761 |
utest_rand64(rng, eslRAND64_DOUBLE_OPEN); |
| 762 |
|
| 763 |
utest_Deal(rng); |
| 764 |
|
| 765 |
fprintf(stderr, "# status = ok\n"); |
| 766 |
} |
| 767 |
|
| 768 |
esl_rand64_Destroy(rng); |
| 769 |
esl_getopts_Destroy(go); |
| 770 |
return 0; |
| 771 |
} |
| 772 |
|
| 773 |
|
| 774 |
|
| 775 |
|
| 776 |
|
| 777 |
|
| 778 |
|
| 779 |
static int |
| 780 |
save_bitfile(char *bitfile, ESL_RAND64 *rng) |
| 781 |
{ |
| 782 |
FILE *fp = NULL; |
| 783 |
uint64_t u; |
| 784 |
int n = 400000; |
| 785 |
int i,b; |
| 786 |
|
| 787 |
if (( fp = fopen(bitfile, "w")) == NULL) |
| 788 |
esl_fatal("failed to open bitfile %s for writing", bitfile); |
| 789 |
|
| 790 |
for (i = 0; i < n; i++) |
| 791 |
{ |
| 792 |
u = esl_rand64(rng); |
| 793 |
for (b = 0; b < 64; b++) |
| 794 |
{ |
| 795 |
if (u & 0x1) fprintf(fp, "1"); |
| 796 |
else fprintf(fp, "0"); |
| 797 |
u >>= 1; |
| 798 |
} |
| 799 |
fprintf(fp, "\n"); |
| 800 |
} |
| 801 |
fclose(fp); |
| 802 |
return eslOK; |
| 803 |
} |
| 804 |
#endif |
| 805 |
|
| 806 |
|
| 807 |
|
| 808 |
|
| 809 |
|
| 810 |
|
| 811 |
#ifdef eslRAND64_EXAMPLE |
| 812 |
|
| 813 |
#include "easel.h" |
| 814 |
#include "esl_getopts.h" |
| 815 |
#include "esl_rand64.h" |
| 816 |
|
| 817 |
static ESL_OPTIONS options[] = { |
| 818 |
|
| 819 |
{"-h", eslARG_NONE, FALSE, NULL, NULL, NULL, NULL, NULL, "show help and usage", 0}, |
| 820 |
{"--seed", eslARG_INT, "0", NULL, NULL, NULL, NULL, NULL, "set random number seed to <n>", 0}, |
| 821 |
{ 0,0,0,0,0,0,0,0,0,0}, |
| 822 |
}; |
| 823 |
static char usage[] = "[-options]"; |
| 824 |
static char banner[] = "example driver for rand64"; |
| 825 |
|
| 826 |
int |
| 827 |
main(int argc, char **argv) |
| 828 |
{ |
| 829 |
ESL_GETOPTS *go = esl_getopts_CreateDefaultApp(options, 0, argc, argv, banner, usage); |
| 830 |
ESL_RAND64 *rng = esl_rand64_Create(esl_opt_GetInteger(go, "--seed")); |
| 831 |
int i; |
| 832 |
|
| 833 |
for (i = 0; i < 1000; i++) |
| 834 |
printf("%20" PRIu64 "\n", esl_rand64(rng)); |
| 835 |
|
| 836 |
esl_rand64_Destroy(rng); |
| 837 |
esl_getopts_Destroy(go); |
| 838 |
return 0; |
| 839 |
} |
| 840 |
#endif |
| 841 |
|
| 842 |
|
| 843 |
|