random.h 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  1. /* xdelta3 - delta compression tools and library -*- Mode: C++ -*-
  2. Copyright 2016 Joshua MacDonald
  3. Licensed under the Apache License, Version 2.0 (the "License");
  4. you may not use this file except in compliance with the License.
  5. You may obtain a copy of the License at
  6. http://www.apache.org/licenses/LICENSE-2.0
  7. Unless required by applicable law or agreed to in writing, software
  8. distributed under the License is distributed on an "AS IS" BASIS,
  9. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  10. See the License for the specific language governing permissions and
  11. limitations under the License.
  12. */
  13. #undef MT_LEN
  14. #undef MT_IA
  15. class MTRandom {
  16. public:
  17. enum Constants {
  18. MT_LEN = 624,
  19. MT_IA = 397
  20. };
  21. static const uint32_t TEST_SEED1;
  22. static const uint32_t UPPER_MASK;
  23. static const uint32_t LOWER_MASK;
  24. static const uint32_t MATRIX_A;
  25. MTRandom() {
  26. Init(TEST_SEED1);
  27. }
  28. explicit MTRandom(uint32_t seed) {
  29. Init(seed);
  30. }
  31. /* This Mersenne Twister code is attributed to Michael Brundage. Thanks!
  32. * http://www.qbrundage.com/michaelb/pubs/essays/random_number_generation.html
  33. */
  34. uint32_t Rand32 () {
  35. uint32_t y;
  36. static unsigned long mag01[2] = {
  37. 0 , MATRIX_A
  38. };
  39. if (mt_index_ >= MT_LEN) {
  40. int kk;
  41. for (kk = 0; kk < MT_LEN - MT_IA; kk++) {
  42. y = (mt_buffer_[kk] & UPPER_MASK) | (mt_buffer_[kk + 1] & LOWER_MASK);
  43. mt_buffer_[kk] = mt_buffer_[kk + MT_IA] ^ (y >> 1) ^ mag01[y & 0x1UL];
  44. }
  45. for (;kk < MT_LEN - 1; kk++) {
  46. y = (mt_buffer_[kk] & UPPER_MASK) | (mt_buffer_[kk + 1] & LOWER_MASK);
  47. mt_buffer_[kk] = mt_buffer_[kk + (MT_IA - MT_LEN)] ^ (y >> 1) ^ mag01[y & 0x1UL];
  48. }
  49. y = (mt_buffer_[MT_LEN - 1] & UPPER_MASK) | (mt_buffer_[0] & LOWER_MASK);
  50. mt_buffer_[MT_LEN - 1] = mt_buffer_[MT_IA - 1] ^ (y >> 1) ^ mag01[y & 0x1UL];
  51. mt_index_ = 0;
  52. }
  53. y = mt_buffer_[mt_index_++];
  54. y ^= (y >> 11);
  55. y ^= (y << 7) & 0x9d2c5680UL;
  56. y ^= (y << 15) & 0xefc60000UL;
  57. y ^= (y >> 18);
  58. return y;
  59. }
  60. uint32_t ExpRand32(uint32_t mean) {
  61. double mean_d = mean;
  62. double erand = log (1.0 / (Rand32() / (double)UINT32_MAX));
  63. uint32_t x = (uint32_t) (mean_d * erand + 0.5);
  64. return x;
  65. }
  66. uint64_t Rand64() {
  67. return ((uint64_t)Rand32() << 32) | Rand32();
  68. }
  69. uint64_t ExpRand64(uint64_t mean) {
  70. double mean_d = mean;
  71. double erand = log (1.0 / (Rand64() / (double)UINT32_MAX));
  72. uint64_t x = (uint64_t) (mean_d * erand + 0.5);
  73. return x;
  74. }
  75. template <typename T>
  76. T Rand() {
  77. switch (sizeof(T)) {
  78. case sizeof(uint32_t):
  79. return Rand32();
  80. case sizeof(uint64_t):
  81. return Rand64();
  82. default:
  83. cerr << "Invalid sizeof T" << endl;
  84. abort();
  85. }
  86. }
  87. template <typename T>
  88. T ExpRand(T mean) {
  89. switch (sizeof(T)) {
  90. case sizeof(uint32_t):
  91. return ExpRand32(mean);
  92. case sizeof(uint64_t):
  93. return ExpRand64(mean);
  94. default:
  95. cerr << "Invalid sizeof T" << endl;
  96. abort();
  97. }
  98. }
  99. private:
  100. void Init(uint32_t seed) {
  101. mt_buffer_[0] = seed;
  102. mt_index_ = MT_LEN;
  103. for (int i = 1; i < MT_LEN; i++) {
  104. /* See Knuth TAOCP Vol2. 3rd Ed. P.106 for multiplier. */
  105. /* In the previous versions, MSBs of the seed affect */
  106. /* only MSBs of the array mt[]. */
  107. /* 2002/01/09 modified by Makoto Matsumoto */
  108. mt_buffer_[i] =
  109. (1812433253UL * (mt_buffer_[i-1] ^ (mt_buffer_[i-1] >> 30)) + i);
  110. }
  111. }
  112. int mt_index_;
  113. uint32_t mt_buffer_[MT_LEN];
  114. };
  115. const uint32_t MTRandom::TEST_SEED1 = 5489UL;
  116. const uint32_t MTRandom::UPPER_MASK = 0x80000000;
  117. const uint32_t MTRandom::LOWER_MASK = 0x7FFFFFFF;
  118. const uint32_t MTRandom::MATRIX_A = 0x9908B0DF;
  119. class MTRandom8 {
  120. public:
  121. MTRandom8(MTRandom *rand)
  122. : rand_(rand) {
  123. }
  124. uint8_t Rand8() {
  125. uint32_t r = rand_->Rand32();
  126. // TODO: make this use a single byte at a time?
  127. return (r & 0xff) ^ (r >> 7) ^ (r >> 15) ^ (r >> 21);
  128. }
  129. private:
  130. MTRandom *rand_;
  131. };