Team Fortress 2 Source Code as on 22/4/2020
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

123 lines
4.4 KiB

  1. //========= Copyright Valve Corporation, All rights reserved. ============//
  2. //
  3. // Purpose: Random number generator
  4. //
  5. // $Workfile: $
  6. // $NoKeywords: $
  7. //===========================================================================//
  8. #ifndef VSTDLIB_RANDOM_H
  9. #define VSTDLIB_RANDOM_H
  10. #include "vstdlib/vstdlib.h"
  11. #include "tier0/basetypes.h"
  12. #include "tier0/threadtools.h"
  13. #include "tier1/interface.h"
  14. #define NTAB 32
  15. #pragma warning(push)
  16. #pragma warning( disable:4251 )
  17. //-----------------------------------------------------------------------------
  18. // A generator of uniformly distributed random numbers
  19. //-----------------------------------------------------------------------------
  20. class VSTDLIB_CLASS IUniformRandomStream
  21. {
  22. public:
  23. //virtual ~IUniformRandomStream() { }
  24. // Sets the seed of the random number generator
  25. virtual void SetSeed( int iSeed ) = 0;
  26. // Generates random numbers
  27. virtual float RandomFloat( float flMinVal = 0.0f, float flMaxVal = 1.0f ) = 0;
  28. virtual int RandomInt( int iMinVal, int iMaxVal ) = 0;
  29. virtual float RandomFloatExp( float flMinVal = 0.0f, float flMaxVal = 1.0f, float flExponent = 1.0f ) = 0;
  30. };
  31. //-----------------------------------------------------------------------------
  32. // The standard generator of uniformly distributed random numbers
  33. //-----------------------------------------------------------------------------
  34. class VSTDLIB_CLASS CUniformRandomStream : public IUniformRandomStream
  35. {
  36. public:
  37. CUniformRandomStream();
  38. // Sets the seed of the random number generator
  39. virtual void SetSeed( int iSeed );
  40. // Generates random numbers
  41. virtual float RandomFloat( float flMinVal = 0.0f, float flMaxVal = 1.0f );
  42. virtual int RandomInt( int iMinVal, int iMaxVal );
  43. virtual float RandomFloatExp( float flMinVal = 0.0f, float flMaxVal = 1.0f, float flExponent = 1.0f );
  44. private:
  45. int GenerateRandomNumber();
  46. int m_idum;
  47. int m_iy;
  48. int m_iv[NTAB];
  49. CThreadFastMutex m_mutex;
  50. };
  51. //-----------------------------------------------------------------------------
  52. // A generator of gaussian distributed random numbers
  53. //-----------------------------------------------------------------------------
  54. class VSTDLIB_CLASS CGaussianRandomStream
  55. {
  56. public:
  57. // Passing in NULL will cause the gaussian stream to use the
  58. // installed global random number generator
  59. CGaussianRandomStream( IUniformRandomStream *pUniformStream = NULL );
  60. // Attaches to a random uniform stream
  61. void AttachToStream( IUniformRandomStream *pUniformStream = NULL );
  62. // Generates random numbers
  63. float RandomFloat( float flMean = 0.0f, float flStdDev = 1.0f );
  64. private:
  65. IUniformRandomStream *m_pUniformStream;
  66. bool m_bHaveValue;
  67. float m_flRandomValue;
  68. CThreadFastMutex m_mutex;
  69. };
  70. //-----------------------------------------------------------------------------
  71. // A couple of convenience functions to access the library's global uniform stream
  72. //-----------------------------------------------------------------------------
  73. VSTDLIB_INTERFACE void RandomSeed( int iSeed );
  74. VSTDLIB_INTERFACE float RandomFloat( float flMinVal = 0.0f, float flMaxVal = 1.0f );
  75. VSTDLIB_INTERFACE float RandomFloatExp( float flMinVal = 0.0f, float flMaxVal = 1.0f, float flExponent = 1.0f );
  76. VSTDLIB_INTERFACE int RandomInt( int iMinVal, int iMaxVal );
  77. VSTDLIB_INTERFACE float RandomGaussianFloat( float flMean = 0.0f, float flStdDev = 1.0f );
  78. //-----------------------------------------------------------------------------
  79. // IUniformRandomStream interface for free functions
  80. //-----------------------------------------------------------------------------
  81. class VSTDLIB_CLASS CDefaultUniformRandomStream : public IUniformRandomStream
  82. {
  83. public:
  84. virtual void SetSeed( int iSeed ) OVERRIDE { RandomSeed( iSeed ); }
  85. virtual float RandomFloat( float flMinVal, float flMaxVal ) OVERRIDE { return ::RandomFloat( flMinVal, flMaxVal ); }
  86. virtual int RandomInt( int iMinVal, int iMaxVal ) OVERRIDE { return ::RandomInt( iMinVal, iMaxVal ); }
  87. virtual float RandomFloatExp( float flMinVal, float flMaxVal, float flExponent ) OVERRIDE { return ::RandomFloatExp( flMinVal, flMaxVal, flExponent ); }
  88. };
  89. //-----------------------------------------------------------------------------
  90. // Installs a global random number generator, which will affect the Random functions above
  91. //-----------------------------------------------------------------------------
  92. VSTDLIB_INTERFACE void InstallUniformRandomStream( IUniformRandomStream *pStream );
  93. #pragma warning(pop)
  94. #endif // VSTDLIB_RANDOM_H