mRandom.h
Classes:
class
Base class for random number generators.
class
Linear Congruential Method, the "minimal standard generator".
class
Fast, very good random numbers.
Public Typedefs
MRandom
Public Variables
Detailed Description
Public Typedefs
typedef MRandomLCG MRandom
Public Variables
MRandomLCG gRandGen
1 2//----------------------------------------------------------------------------- 3// Copyright (c) 2012 GarageGames, LLC 4// 5// Permission is hereby granted, free of charge, to any person obtaining a copy 6// of this software and associated documentation files (the "Software"), to 7// deal in the Software without restriction, including without limitation the 8// rights to use, copy, modify, merge, publish, distribute, sublicense, and/or 9// sell copies of the Software, and to permit persons to whom the Software is 10// furnished to do so, subject to the following conditions: 11// 12// The above copyright notice and this permission notice shall be included in 13// all copies or substantial portions of the Software. 14// 15// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING 20// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS 21// IN THE SOFTWARE. 22//----------------------------------------------------------------------------- 23 24#ifndef _MRANDOM_H_ 25#define _MRANDOM_H_ 26 27#ifndef _PLATFORMASSERT_H_ 28#include "platform/platformAssert.h" 29#endif 30 31 32//-------------------------------------- 33/// Base class for random number generators 34class MRandomGenerator 35{ 36protected: 37 MRandomGenerator() :mSeed(-1) {} 38 S32 mSeed; 39 40public: 41 virtual ~MRandomGenerator() {} 42 43 void setSeed(); 44 S32 getSeed() { return mSeed; } 45 virtual void setSeed(S32 s) = 0; 46 47 virtual U32 randI( void ) = 0; ///< 0..2^31 random number generator 48 virtual F32 randF( void ); ///< 0.0 .. 1.0 F32 random number generator 49 S32 randI(S32 i, S32 n); ///< i..n integer random number generator 50 F32 randF(F32 i, F32 n); ///< i..n F32 random number generator 51}; 52 53 54//-------------------------------------- 55inline F32 MRandomGenerator::randF() 56{ 57 // default: multiply by 1/(2^31) 58 return F32(randI()) * (1.0f / S32_MAX); 59} 60 61inline S32 MRandomGenerator::randI(S32 i, S32 n) 62{ 63 AssertFatal(i<=<a href="/coding/file/cmdscan_8cpp/#cmdscan_8cpp_1aeab71244afb687f16d8c4f5ee9d6ef0e">n</a>, "MRandomGenerator::randi: inverted range."); 64 return (S32)(i + (randI() % (n - i + 1)) ); 65} 66 67inline F32 MRandomGenerator::randF(F32 i, F32 n) 68{ 69 AssertFatal(i<=<a href="/coding/file/cmdscan_8cpp/#cmdscan_8cpp_1aeab71244afb687f16d8c4f5ee9d6ef0e">n</a>, "MRandomGenerator::randf: inverted range."); 70 return (i + (n - i) * randF()); 71} 72 73 74//-------------------------------------- 75/// Linear Congruential Method, the "minimal standard generator" 76/// 77/// Fast, farly good random numbers (better than using rand) 78/// 79/// @author Park & Miller, 1988, Comm of the ACM, 31(10), pp. 1192-1201 80class MRandomLCG : public MRandomGenerator 81{ 82protected: 83 static const S32 msQuotient; 84 static const S32 msRemainder; 85 86public: 87 MRandomLCG(); 88 MRandomLCG(S32 s); 89 virtual ~MRandomLCG() {} 90 91 static void setGlobalRandSeed(U32 seed); 92 93 void setSeed(S32 s); 94// using MRandomGenerator::randI; 95 S32 randI(S32 i, S32 n); ///< i..n integer generator 96 97 U32 randI( void ); 98 99}; 100 101// Solution to "using" problem. 102inline S32 MRandomLCG::randI(S32 i, S32 n) 103{ 104 return( MRandomGenerator::randI(i,n) ); 105} 106 107 108//-------------------------------------- 109/// Fast, very good random numbers 110/// 111/// Period = 2^249 112/// 113/// Kirkpatrick, S., and E. Stoll, 1981; A Very Fast Shift-Register 114/// Sequence Random Number Generator, Journal of Computational Physics, 115/// V. 40. 116/// 117/// Maier, W.L., 1991; A Fast Pseudo Random Number Generator, 118/// Dr. Dobb's Journal, May, pp. 152 - 157 119class MRandomR250: public MRandomGenerator 120{ 121private: 122 U32 mBuffer[250]; 123 S32 mIndex; 124 125public: 126 MRandomR250(); 127 MRandomR250(S32 s); 128 virtual ~MRandomR250() {} 129 130 void setSeed(S32 s); 131// using MRandomGenerator::randI; 132 U32 randI(); 133}; 134 135 136typedef MRandomLCG MRandom; 137 138extern MRandomLCG gRandGen; 139 140 141#endif //_MRANDOM_H_ 142