mRandom.h 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  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. #ifndef _MRANDOM_H_
  23. #define _MRANDOM_H_
  24. #ifndef _PLATFORM_H_
  25. #include "platform/platform.h"
  26. #endif
  27. #include "algorithm/pcg_basic.h"
  28. //-----------------------------------------------------------------------------
  29. class RandomGeneratorBase
  30. {
  31. protected:
  32. RandomGeneratorBase() {}
  33. S32 mSeed;
  34. public:
  35. void resetSeed( void );
  36. inline S32 getSeed( void ) const { return mSeed; }
  37. inline F32 randRangeF( const F32 from, const F32 to ) { AssertFatal( from <= to, "RandomGeneratorBase::randF() - Inverted range." ); return (from + (to - from) * randF()); }
  38. inline S32 randRangeI( const S32 from, const S32 to ) { AssertFatal( from <= to, "RandomGeneratorBase::randI() - Inverted range." ); return (S32)(from + (randI() % (to - from + 1)) ); }
  39. virtual F32 randF( void ) { return F32(randI()) * F32(1.0/2147483647.0); }
  40. virtual U32 randI( void ) = 0;
  41. virtual void setSeed( const S32 seed ) = 0;
  42. };
  43. //-----------------------------------------------------------------------------
  44. /// Linear Congruential Method, the "minimal standard generator"
  45. ///
  46. /// Fast, fairly good random numbers (better than using rand)
  47. ///
  48. /// @author Park & Miller, 1988, Comm of the ACM, 31(10), pp. 1192-1201
  49. //-----------------------------------------------------------------------------
  50. class RandomLCG : public RandomGeneratorBase
  51. {
  52. protected:
  53. static const S32 msQuotient;
  54. static const S32 msRemainder;
  55. public:
  56. RandomLCG();
  57. RandomLCG( const S32 seed );
  58. static void setGlobalRandSeed( const U32 seed );
  59. void setSeed( const S32 seed );
  60. U32 randI( void );
  61. };
  62. //-----------------------------------------------------------------------------
  63. /// Fast, very good random numbers
  64. ///
  65. /// Period = 2^249
  66. ///
  67. /// Kirkpatrick, S., and E. Stoll, 1981; A Very Fast Shift-Register
  68. /// Sequence Random Number Generator, Journal of Computational Physics,
  69. /// V. 40.
  70. ///
  71. /// Maier, W.L., 1991; A Fast Pseudo Random Number Generator,
  72. /// Dr. Dobb's Journal, May, pp. 152 - 157
  73. //-----------------------------------------------------------------------------
  74. class RandomR250 : public RandomGeneratorBase
  75. {
  76. private:
  77. U32 mBuffer[250];
  78. S32 mIndex;
  79. public:
  80. RandomR250();
  81. RandomR250( const S32 seed );
  82. void setSeed( const S32 seed );
  83. U32 randI( void );
  84. };
  85. //-----------------------------------------------------------------------------
  86. //-----------------------------------------------------------------------------
  87. /// PCG Random Number Generator
  88. ///
  89. /// Fast and statistically excellent random numbers
  90. ///
  91. /// Period = 2^64
  92. ///
  93. /// Copyright 2014 Melissa O'Neill <[email protected]>
  94. ///
  95. /// Licensed under the Apache License, Version 2.0
  96. //-----------------------------------------------------------------------------
  97. class RandomPCG : public RandomGeneratorBase
  98. {
  99. private:
  100. pcg32_random_t rng;
  101. public:
  102. RandomPCG();
  103. RandomPCG(const S32 seed);
  104. RandomPCG(const S32 seed, const S32 stream);
  105. void setSeed(const S32 seed);
  106. void setSeed(const S32 seed, const S32 stream);
  107. U32 randI(void) { return static_cast<U32>(pcg32_random_r(&rng)); }
  108. U32 randI(U32 bound) { return static_cast<U32>(pcg32_boundedrand_r(&rng, static_cast<uint32_t>(bound))); }
  109. };
  110. //-----------------------------------------------------------------------------
  111. extern RandomLCG gRandGen;
  112. #endif //_MRANDOM_H_