mRandom.cpp 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  1. //-----------------------------------------------------------------------------
  2. // Copyright (c) 2012 GarageGames, LLC
  3. //
  4. // Permission is hereby granted, free of charge, to any person obtaining a copy
  5. // of this software and associated documentation files (the "Software"), to
  6. // deal in the Software without restriction, including without limitation the
  7. // rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
  8. // sell copies of the Software, and to permit persons to whom the Software is
  9. // furnished to do so, subject to the following conditions:
  10. //
  11. // The above copyright notice and this permission notice shall be included in
  12. // all copies or substantial portions of the Software.
  13. //
  14. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  16. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  17. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  18. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  19. // FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
  20. // IN THE SOFTWARE.
  21. //-----------------------------------------------------------------------------
  22. #include "platform/platform.h"
  23. #include "math/mRandom.h"
  24. #include "core/util/journal/journal.h"
  25. MRandomLCG gRandGen;
  26. U32 gRandGenSeed = 1376312589;
  27. void MRandomLCG::setGlobalRandSeed(U32 seed)
  28. {
  29. if (Journal::IsPlaying())
  30. Journal::Read(&gRandGenSeed);
  31. else
  32. {
  33. gRandGenSeed = seed;
  34. if (Journal::IsRecording())
  35. Journal::Write(gRandGenSeed);
  36. }
  37. //now actually set the seed
  38. gRandGen.setSeed(gRandGenSeed);
  39. }
  40. static U32 msSeed = 1376312589;
  41. inline U32 generateSeed()
  42. {
  43. // A very, VERY crude LCG but good enough to generate
  44. // a nice range of seed values
  45. msSeed = (msSeed * 0x015a4e35L) + 1;
  46. msSeed = (msSeed>>16)&0x7fff;
  47. return (msSeed);
  48. }
  49. //--------------------------------------
  50. void MRandomGenerator::setSeed()
  51. {
  52. setSeed(generateSeed());
  53. }
  54. //--------------------------------------
  55. const S32 MRandomLCG::msQuotient = S32_MAX / 16807L;
  56. const S32 MRandomLCG::msRemainder = S32_MAX % 16807L;
  57. //--------------------------------------
  58. MRandomLCG::MRandomLCG()
  59. {
  60. setSeed(generateSeed());
  61. }
  62. MRandomLCG::MRandomLCG(S32 s)
  63. {
  64. setSeed(s);
  65. }
  66. //--------------------------------------
  67. void MRandomLCG::setSeed(S32 s)
  68. {
  69. mSeed = s;
  70. }
  71. //--------------------------------------
  72. U32 MRandomLCG::randI()
  73. {
  74. if ( mSeed <= msQuotient )
  75. mSeed = (mSeed * 16807) % S32_MAX;
  76. else
  77. {
  78. S32 high_part = mSeed / msQuotient;
  79. S32 low_part = mSeed % msQuotient;
  80. S32 test = (16807 * low_part) - (msRemainder * high_part);
  81. if ( test > 0 )
  82. mSeed = test;
  83. else
  84. mSeed = test + S32_MAX;
  85. }
  86. return mSeed;
  87. }
  88. //--------------------------------------
  89. MRandomR250::MRandomR250()
  90. {
  91. setSeed(generateSeed());
  92. }
  93. MRandomR250::MRandomR250(S32 s)
  94. {
  95. setSeed(s);
  96. }
  97. //--------------------------------------
  98. void MRandomR250::setSeed(S32 s)
  99. {
  100. mSeed = s;
  101. MRandomLCG lcg( s );
  102. mIndex = 0;
  103. S32 j;
  104. for (j = 0; j < 250; j++) // fill r250 buffer with bit values
  105. mBuffer[j] = lcg.randI();
  106. for (j = 0; j < 250; j++) // set some MSBs to 1
  107. if ( lcg.randI() > 0x40000000L )
  108. mBuffer[j] |= 0x80000000L;
  109. U32 msb = 0x80000000; // turn on diagonal bit
  110. U32 mask = 0xffffffff; // turn off the leftmost bits
  111. for (j = 0; j < 32; j++)
  112. {
  113. S32 k = 7 * j + 3; // select a word to operate on
  114. mBuffer[k] &= mask; // turn off bits left of the diagonal
  115. mBuffer[k] |= msb; // turn on the diagonal bit
  116. mask >>= 1;
  117. msb >>= 1;
  118. }
  119. }
  120. //--------------------------------------
  121. U32 MRandomR250::randI()
  122. {
  123. S32 j;
  124. // wrap pointer around
  125. if ( mIndex >= 147 ) j = mIndex - 147;
  126. else j = mIndex + 103;
  127. U32 new_rand = mBuffer[ mIndex ] ^ mBuffer[ j ];
  128. mBuffer[ mIndex ] = new_rand;
  129. // increment pointer for next time
  130. if ( mIndex >= 249 ) mIndex = 0;
  131. else mIndex++;
  132. return new_rand >> 1;
  133. }