MTRand.cpp 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  1. /*
  2. Copyright (C) 1997 - 2002, Makoto Matsumoto and Takuji Nishimura,
  3. All rights reserved.
  4. Redistribution and use in source and binary forms, with or without
  5. modification, are permitted provided that the following conditions
  6. are met:
  7. 1. Redistributions of source code must retain the above copyright
  8. notice, this list of conditions and the following disclaimer.
  9. 2. Redistributions in binary form must reproduce the above copyright
  10. notice, this list of conditions and the following disclaimer in the
  11. documentation and/or other materials provided with the distribution.
  12. 3. The names of its contributors may not be used to endorse or promote
  13. products derived from this software without specific prior written
  14. permission.
  15. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  16. "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  17. LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  18. A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
  19. CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
  20. EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
  21. PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
  22. PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
  23. LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
  24. NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  25. SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  26. Any feedback is very welcome.
  27. http://www.math.sci.hiroshima-u.ac.jp/~m-mat/MT/emt.html
  28. email: m-mat @ math.sci.hiroshima-u.ac.jp (remove space)
  29. */
  30. #include "MTRand.h"
  31. USING_NS_BF;
  32. /* Period parameters */
  33. #define MTRAND_M 397
  34. #define MATRIX_A 0x9908b0dfUL /* constant vector a */
  35. #define UPPER_MASK 0x80000000UL /* most significant w-r bits */
  36. #define LOWER_MASK 0x7fffffffUL /* least significant r bits */
  37. /* Tempering parameters */
  38. #define TEMPERING_MASK_B 0x9d2c5680
  39. #define TEMPERING_MASK_C 0xefc60000
  40. #define TEMPERING_SHIFT_U(y) (y >> 11)
  41. #define TEMPERING_SHIFT_S(y) (y << 7)
  42. #define TEMPERING_SHIFT_T(y) (y << 15)
  43. #define TEMPERING_SHIFT_L(y) (y >> 18)
  44. MTRand::MTRand(const std::string& theSerialData)
  45. {
  46. SRand(theSerialData);
  47. mti = MTRAND_N + 1; /* mti==MTRAND_N+1 means mt[MTRAND_N] is not initialized */
  48. }
  49. MTRand::MTRand(unsigned long seed)
  50. {
  51. SRand(seed);
  52. }
  53. MTRand::MTRand()
  54. {
  55. SRand(4357);
  56. }
  57. void MTRand::SRand(const std::string& theSerialData)
  58. {
  59. if (theSerialData.size() == MTRAND_N * 4)
  60. {
  61. memcpy(mt, theSerialData.c_str(), MTRAND_N * 4);
  62. }
  63. else
  64. SRand(4357);
  65. }
  66. void MTRand::SRand(unsigned long seed)
  67. {
  68. if (seed == 0)
  69. seed = 4357;
  70. /* setting initial seeds to mt[MTRAND_N] using */
  71. /* the generator Line 25 of Table 1 in */
  72. /* [KNUTH 1981, The Art of Computer Programming */
  73. /* Vol. 2 (2nd Ed.), pp102] */
  74. mt[0] = seed & 0xffffffffUL;
  75. for (mti = 1; mti < MTRAND_N; mti++)
  76. {
  77. mt[mti] =
  78. (1812433253UL * (mt[mti - 1] ^ (mt[mti - 1] >> 30)) + mti);
  79. /* See Knuth TAOCP Vol2. 3rd Ed. P.106 for multiplier. */
  80. /* In the previous versions, MSBs of the seed affect */
  81. /* only MSBs of the array mt[]. */
  82. /* 2002/01/09 modified by Makoto Matsumoto */
  83. mt[mti] &= 0xffffffffUL;
  84. /* for >32 bit machines */
  85. }
  86. }
  87. unsigned long MTRand::Next()
  88. {
  89. unsigned long y;
  90. static unsigned long mag01[2] = { 0x0, MATRIX_A };
  91. /* mag01[x] = x * MATRIX_A for x=0,1 */
  92. if (mti >= MTRAND_N) { /* generate MTRAND_N words at one time */
  93. int kk;
  94. for (kk = 0; kk < MTRAND_N - MTRAND_M; kk++) {
  95. y = (mt[kk] & UPPER_MASK) | (mt[kk + 1] & LOWER_MASK);
  96. mt[kk] = mt[kk + MTRAND_M] ^ (y >> 1) ^ mag01[y & 0x1UL];
  97. }
  98. for (; kk < MTRAND_N - 1; kk++) {
  99. y = (mt[kk] & UPPER_MASK) | (mt[kk + 1] & LOWER_MASK);
  100. mt[kk] = mt[kk + (MTRAND_M - MTRAND_N)] ^ (y >> 1) ^ mag01[y & 0x1UL];
  101. }
  102. y = (mt[MTRAND_N - 1] & UPPER_MASK) | (mt[0] & LOWER_MASK);
  103. mt[MTRAND_N - 1] = mt[MTRAND_M - 1] ^ (y >> 1) ^ mag01[y & 0x1UL];
  104. mti = 0;
  105. }
  106. y = mt[mti++];
  107. y ^= TEMPERING_SHIFT_U(y);
  108. y ^= TEMPERING_SHIFT_S(y) & TEMPERING_MASK_B;
  109. y ^= TEMPERING_SHIFT_T(y) & TEMPERING_MASK_C;
  110. y ^= TEMPERING_SHIFT_L(y);
  111. y &= 0x7FFFFFFF;
  112. /*char aStr[256];
  113. sprintf(aStr, "Rand=%d\r\n", y);
  114. OutputDebugString(aStr);*/
  115. return y;
  116. }
  117. unsigned long MTRand::Next(unsigned long range)
  118. {
  119. return Next() % range;
  120. }
  121. String MTRand::Serialize()
  122. {
  123. String aString;
  124. aString.Append(' ', MTRAND_N * 4);
  125. memcpy((char*)aString.c_str(), mt, MTRAND_N * 4);
  126. return aString;
  127. }