mRandom.cc 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207
  1. //-----------------------------------------------------------------------------
  2. // Copyright (c) 2013 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 "platform/event.h"
  24. #include "game/gameInterface.h"
  25. #include "math/mRandom.h"
  26. //-----------------------------------------------------------------------------
  27. RandomLCG gRandGen;
  28. static U32 msSeed = 1376312589;
  29. U32 gRandGenSeed = msSeed;
  30. const S32 RandomLCG::msQuotient = S32_MAX / 16807L;
  31. const S32 RandomLCG::msRemainder = S32_MAX % 16807L;
  32. //-----------------------------------------------------------------------------
  33. inline U32 generateSeed()
  34. {
  35. // A very, VERY crude LCG but good enough to generate a nice range of seed values
  36. msSeed = (msSeed * 0x015a4e35L) + 1;
  37. msSeed = (msSeed>>16)&0x7fff;
  38. return msSeed;
  39. }
  40. //-----------------------------------------------------------------------------
  41. void RandomGeneratorBase::resetSeed( void )
  42. {
  43. setSeed( generateSeed() );
  44. }
  45. //-----------------------------------------------------------------------------
  46. RandomLCG::RandomLCG()
  47. {
  48. setSeed(generateSeed());
  49. }
  50. //-----------------------------------------------------------------------------
  51. RandomLCG::RandomLCG( const S32 seed )
  52. {
  53. setSeed( seed );
  54. }
  55. //-----------------------------------------------------------------------------
  56. void RandomLCG::setGlobalRandSeed( const U32 seed )
  57. {
  58. #ifdef TORQUE_ALLOW_JOURNALING
  59. if (Game->isJournalReading())
  60. Game->journalRead(&gRandGenSeed);
  61. else
  62. {
  63. gRandGenSeed = seed;
  64. if (Game->isJournalWriting())
  65. Game->journalWrite(gRandGenSeed);
  66. }
  67. #else
  68. gRandGenSeed = seed;
  69. #endif //TORQUE_ALLOW_JOURNALING
  70. //now actually set the seed
  71. gRandGen.setSeed( gRandGenSeed );
  72. }
  73. //-----------------------------------------------------------------------------
  74. void RandomLCG::setSeed( const S32 seed )
  75. {
  76. mSeed = seed;
  77. }
  78. //-----------------------------------------------------------------------------
  79. U32 RandomLCG::randI()
  80. {
  81. if ( mSeed <= msQuotient )
  82. {
  83. mSeed = (mSeed * 16807L) % S32_MAX;
  84. }
  85. else
  86. {
  87. const S32 high_part = mSeed / msQuotient;
  88. const S32 low_part = mSeed % msQuotient;
  89. const S32 test = (16807L * low_part) - (msRemainder * high_part);
  90. if ( test > 0 )
  91. mSeed = test;
  92. else
  93. mSeed = test + S32_MAX;
  94. }
  95. return mSeed;
  96. }
  97. //-----------------------------------------------------------------------------
  98. RandomR250::RandomR250()
  99. {
  100. setSeed( generateSeed() );
  101. }
  102. //-----------------------------------------------------------------------------
  103. RandomR250::RandomR250( const S32 seed )
  104. {
  105. setSeed( seed );
  106. }
  107. //-----------------------------------------------------------------------------
  108. void RandomR250::setSeed( const S32 seed )
  109. {
  110. mSeed = seed;
  111. RandomLCG lcg( seed );
  112. mIndex = 0;
  113. S32 j;
  114. for (j = 0; j < 250; j++) // fill r250 buffer with bit values
  115. mBuffer[j] = lcg.randI();
  116. for (j = 0; j < 250; j++) // set some MSBs to 1
  117. if ( lcg.randI() > 0x40000000L )
  118. mBuffer[j] |= 0x80000000L;
  119. U32 msb = 0x80000000; // turn on diagonal bit
  120. U32 mask = 0xffffffff; // turn off the leftmost bits
  121. for (j = 0; j < 32; j++)
  122. {
  123. S32 k = 7 * j + 3; // select a word to operate on
  124. mBuffer[k] &= mask; // turn off bits left of the diagonal
  125. mBuffer[k] |= msb; // turn on the diagonal bit
  126. mask >>= 1;
  127. msb >>= 1;
  128. }
  129. }
  130. //-----------------------------------------------------------------------------
  131. U32 RandomR250::randI( void )
  132. {
  133. S32 j;
  134. // wrap pointer around
  135. if ( mIndex >= 147 ) j = mIndex - 147;
  136. else j = mIndex + 103;
  137. U32 new_rand = mBuffer[ mIndex ] ^ mBuffer[ j ];
  138. mBuffer[ mIndex ] = new_rand;
  139. // increment pointer for next time
  140. if ( mIndex >= 249 ) mIndex = 0;
  141. else mIndex++;
  142. return new_rand >> 1;
  143. }
  144. RandomPCG::RandomPCG()
  145. {
  146. pcg32_srandom_r(&rng, generateSeed(), generateSeed());
  147. }
  148. RandomPCG::RandomPCG(const S32 seed)
  149. {
  150. pcg32_srandom_r(&rng, seed, generateSeed());
  151. }
  152. RandomPCG::RandomPCG(const S32 seed, const S32 stream)
  153. {
  154. pcg32_srandom_r(&rng, seed, stream);
  155. }
  156. void RandomPCG::setSeed(const S32 seed)
  157. {
  158. pcg32_srandom_r(&rng, seed, generateSeed());
  159. }
  160. void RandomPCG::setSeed(const S32 seed, const S32 stream)
  161. {
  162. pcg32_srandom_r(&rng, seed, stream);
  163. }