rand48.c 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  1. #include <stdlib.h>
  2. #include <stdio.h>
  3. #include "test_helpers.h"
  4. int main(void) {
  5. long x_l, x_m;
  6. double x_d;
  7. long seedval = 0xcafebeef;
  8. unsigned short seed[3] = {0xfedc, 0xba98, 0x7654};
  9. unsigned short xsubi[3] = {0xabcd, 0xef42, 0x5678};
  10. unsigned short lcong48_params[7] = {0x0123, 0x4567, 0x89ab, 0xcdef, 0x4242, 0xf000, 0xbaaa};
  11. unsigned short xsubi_from_seed48[3] = {0, 0, 0};
  12. /* Test uninitialized behavior */
  13. printf("lrand48 (uninitialized):");
  14. for (int i = 0; i < 10; i++)
  15. {
  16. x_l = lrand48();
  17. printf(" %ld", x_l);
  18. }
  19. printf("\n");
  20. /* Test different output types with same seed, builtin X_i and
  21. * default multiplier and addend */
  22. srand48(seedval);
  23. printf("drand48 (seeded with srand48):");
  24. for (int i = 0; i < 10; i++)
  25. {
  26. x_d = drand48();
  27. printf(" %lf", x_d);
  28. }
  29. printf("\n");
  30. srand48(seedval);
  31. printf("lrand48 (seeded with srand48):");
  32. for (int i = 0; i < 10; i++)
  33. {
  34. x_l = lrand48();
  35. printf(" %ld", x_l);
  36. }
  37. printf("\n");
  38. srand48(seedval);
  39. printf("mrand48 (seeded with srand48):");
  40. for (int i = 0; i < 10; i++)
  41. {
  42. x_m = mrand48();
  43. printf(" %ld", x_m);
  44. }
  45. printf("\n");
  46. /* Test corresponding functions taking user-supplied X_i, with
  47. * default multiplier and addend */
  48. printf("erand48:");
  49. for (int i = 0; i < 10; i++)
  50. {
  51. x_d = erand48(xsubi);
  52. printf(" %lf", x_d);
  53. }
  54. printf("\n");
  55. printf("nrand48:");
  56. for (int i = 0; i < 10; i++)
  57. {
  58. x_l = nrand48(xsubi);
  59. printf(" %ld", x_l);
  60. }
  61. printf("\n");
  62. printf("jrand48:");
  63. for (int i = 0; i < 10; i++)
  64. {
  65. x_l = jrand48(xsubi);
  66. printf(" %ld", x_l);
  67. }
  68. printf("\n");
  69. /* Test seed48() "stashing" behavior. */
  70. unsigned short *seed48_return = seed48(seed);
  71. printf("seed48_return: [%x, %x, %x]\n",
  72. seed48_return[0], seed48_return[1], seed48_return[2]);
  73. /* Test seeding behavior of seed48() */
  74. printf("lrand48 (seeded with seed48):");
  75. for (int i = 0; i < 10; i++)
  76. {
  77. x_l = lrand48();
  78. printf(" %ld", x_l);
  79. }
  80. printf("\n");
  81. /* Test restore from seed48()'s "stashed" value */
  82. xsubi_from_seed48[0] = seed48_return[0];
  83. xsubi_from_seed48[1] = seed48_return[1];
  84. xsubi_from_seed48[2] = seed48_return[2];
  85. printf("xsubi restored froom seed48 return value: [%x, %x, %x]\n",
  86. xsubi_from_seed48[0], xsubi_from_seed48[1], xsubi_from_seed48[2]);
  87. printf("nrand48 (from xsubi value returned by seed48):");
  88. for (int i = 0; i < 10; i++)
  89. {
  90. x_l = nrand48(xsubi_from_seed48);
  91. printf(" %ld", x_l);
  92. }
  93. printf("\n");
  94. /* Test behavior with all-user-defined parameters */
  95. lcong48(lcong48_params);
  96. printf("lrand48 (with parameters from lcong48):");
  97. for (int i = 0; i < 10; i++)
  98. {
  99. x_l = lrand48();
  100. printf(" %ld", x_l);
  101. }
  102. printf("\n");
  103. /* Test multiplier- and addend-restoring behavior of srand48() */
  104. srand48(seedval);
  105. printf("lrand48 (seeded with srand48 after lcong48 call):");
  106. for (int i = 0; i < 10; i++)
  107. {
  108. x_l = lrand48();
  109. printf(" %ld", x_l);
  110. }
  111. printf("\n");
  112. /* Test multiplier- and addend-restoring behavior of seed48() */
  113. lcong48(lcong48_params);
  114. seed48(seed);
  115. printf("lrand48 (seeded with seed48 after lcong48 call):");
  116. for (int i = 0; i < 10; i++)
  117. {
  118. x_l = lrand48();
  119. printf(" %ld", x_l);
  120. }
  121. printf("\n");
  122. }