Random.cpp 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  1. #include "Random.h"
  2. #include <iostream>
  3. #include "Zeit.h"
  4. using namespace Framework;
  5. RandomGenerator::RandomGenerator()
  6. : ReferenceCounter()
  7. {
  8. int tmp[] = {
  9. 3,
  10. -1726662223,
  11. 379960547,
  12. 1735697613,
  13. 1040273694,
  14. 1313901226,
  15. 1627687941,
  16. -179304937,
  17. -2073333483,
  18. 1780058412,
  19. -1989503057,
  20. -615974602,
  21. 344556628,
  22. 939512070,
  23. -1249116260,
  24. 1507946756,
  25. -812545463,
  26. 154635395,
  27. 1388815473,
  28. -1926676823,
  29. 525320961,
  30. -1009028674,
  31. 968117788,
  32. -123449607,
  33. 1284210865,
  34. 435012392,
  35. -2017506339,
  36. -911064859,
  37. -370259173,
  38. 1132637927,
  39. 1398500161,
  40. -205601318,
  41. };
  42. memcpy(randtbl, tmp, 32 * 4);
  43. unsafe_state
  44. = {&randtbl[4], &randtbl[1], &randtbl[1], 3, 31, 3, &randtbl[32]};
  45. offset = 0;
  46. srand((int)time(0));
  47. }
  48. // Destruktor
  49. RandomGenerator::~RandomGenerator() {}
  50. void RandomGenerator::srand(int seed)
  51. {
  52. this->seed = seed;
  53. int type;
  54. int* state;
  55. long int i;
  56. int word;
  57. int* dst;
  58. int kc;
  59. type = unsafe_state.rand_type;
  60. if ((unsigned int)type >= 5) return;
  61. state = unsafe_state.state;
  62. if (seed == 0) seed = 1;
  63. state[0] = seed;
  64. if (type == 0) return;
  65. dst = state;
  66. word = seed;
  67. kc = unsafe_state.rand_deg;
  68. for (i = 1; i < kc; ++i)
  69. {
  70. long int hi = word / 127773;
  71. long int lo = word % 127773;
  72. word = (int)(16807 * lo - 2836 * hi);
  73. if (word < 0) word += 2147483647;
  74. *++dst = word;
  75. }
  76. unsafe_state.fptr = &state[unsafe_state.rand_sep];
  77. unsafe_state.rptr = &state[0];
  78. kc *= 10;
  79. while (--kc >= 0)
  80. rand();
  81. }
  82. void RandomGenerator::setSeed(__int64 seed)
  83. {
  84. this->offset = 0;
  85. int offset = (int)(seed >> 32);
  86. srand((int)seed);
  87. offset -= this->offset;
  88. while (offset-- > 0)
  89. rand();
  90. }
  91. double RandomGenerator::rand()
  92. {
  93. offset++;
  94. int* state;
  95. state = unsafe_state.state;
  96. if (unsafe_state.rand_type == 0)
  97. {
  98. int val = state[0];
  99. val = ((state[0] * 1103515245) + 12345) & 0x7fffffff;
  100. state[0] = val;
  101. return val / 2147483647.0;
  102. }
  103. else
  104. {
  105. int* fptr = unsafe_state.fptr;
  106. int* rptr = unsafe_state.rptr;
  107. int* end_ptr = unsafe_state.end_ptr;
  108. int val;
  109. val = *fptr += *rptr;
  110. int result = (val >> 1) & 0x7fffffff;
  111. ++fptr;
  112. if (fptr >= end_ptr)
  113. {
  114. fptr = state;
  115. ++rptr;
  116. }
  117. else
  118. {
  119. ++rptr;
  120. if (rptr >= end_ptr) rptr = state;
  121. }
  122. unsafe_state.fptr = fptr;
  123. unsafe_state.rptr = rptr;
  124. double res = result / 2147483647.0;
  125. return res;
  126. }
  127. return 0;
  128. }
  129. __int64 RandomGenerator::getSeed() const
  130. {
  131. __int64 ret = ((__int64)offset << 32) | seed;
  132. return ret;
  133. }