mRandom.h 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  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. //-----------------------------------------------------------------------------
  28. class RandomGeneratorBase
  29. {
  30. protected:
  31. RandomGeneratorBase() {}
  32. S32 mSeed;
  33. public:
  34. void resetSeed( void );
  35. inline S32 getSeed( void ) const { return mSeed; }
  36. inline F32 randRangeF( const F32 from, const F32 to ) { AssertFatal( from <= to, "RandomGeneratorBase::randF() - Inverted range." ); return (from + (to - from) * randF()); }
  37. inline S32 randRangeI( const S32 from, const S32 to ) { AssertFatal( from <= to, "RandomGeneratorBase::randI() - Inverted range." ); return (S32)(from + (randI() % (to - from + 1)) ); }
  38. virtual F32 randF( void ) { return F32(randI()) * F32(1.0/2147483647.0); }
  39. virtual U32 randI( void ) = 0;
  40. virtual void setSeed( const S32 seed ) = 0;
  41. };
  42. //-----------------------------------------------------------------------------
  43. /// Linear Congruential Method, the "minimal standard generator"
  44. ///
  45. /// Fast, fairly good random numbers (better than using rand)
  46. ///
  47. /// @author Park & Miller, 1988, Comm of the ACM, 31(10), pp. 1192-1201
  48. //-----------------------------------------------------------------------------
  49. class RandomLCG : public RandomGeneratorBase
  50. {
  51. protected:
  52. static const S32 msQuotient;
  53. static const S32 msRemainder;
  54. public:
  55. RandomLCG();
  56. RandomLCG( const S32 seed );
  57. static void setGlobalRandSeed( const U32 seed );
  58. void setSeed( const S32 seed );
  59. U32 randI( void );
  60. };
  61. //-----------------------------------------------------------------------------
  62. /// Fast, very good random numbers
  63. ///
  64. /// Period = 2^249
  65. ///
  66. /// Kirkpatrick, S., and E. Stoll, 1981; A Very Fast Shift-Register
  67. /// Sequence Random Number Generator, Journal of Computational Physics,
  68. /// V. 40.
  69. ///
  70. /// Maier, W.L., 1991; A Fast Pseudo Random Number Generator,
  71. /// Dr. Dobb's Journal, May, pp. 152 - 157
  72. //-----------------------------------------------------------------------------
  73. class RandomR250 : public RandomGeneratorBase
  74. {
  75. private:
  76. U32 mBuffer[250];
  77. S32 mIndex;
  78. public:
  79. RandomR250();
  80. RandomR250( const S32 seed );
  81. void setSeed( const S32 seed );
  82. U32 randI( void );
  83. };
  84. //-----------------------------------------------------------------------------
  85. extern RandomLCG gRandGen;
  86. #endif //_MRANDOM_H_