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.

111 lines
3.6 KiB

  1. //===== Copyright (c) 1996-2005, 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 IUniformRandomStream
  21. {
  22. public:
  23. // Sets the seed of the random number generator
  24. virtual void SetSeed( int iSeed ) = 0;
  25. // Generates random numbers
  26. virtual float RandomFloat( float flMinVal = 0.0f, float flMaxVal = 1.0f ) = 0;
  27. virtual int RandomInt( int iMinVal, int iMaxVal ) = 0;
  28. virtual float RandomFloatExp( float flMinVal = 0.0f, float flMaxVal = 1.0f, float flExponent = 1.0f ) = 0;
  29. };
  30. //-----------------------------------------------------------------------------
  31. // The standard generator of uniformly distributed random numbers
  32. //-----------------------------------------------------------------------------
  33. class VSTDLIB_CLASS CUniformRandomStream : public IUniformRandomStream
  34. {
  35. public:
  36. CUniformRandomStream();
  37. // Sets the seed of the random number generator
  38. virtual void SetSeed( int iSeed );
  39. // Generates random numbers
  40. virtual float RandomFloat( float flMinVal = 0.0f, float flMaxVal = 1.0f );
  41. virtual int RandomInt( int iMinVal, int iMaxVal );
  42. virtual float RandomFloatExp( float flMinVal = 0.0f, float flMaxVal = 1.0f, float flExponent = 1.0f );
  43. private:
  44. int GenerateRandomNumber();
  45. int m_idum;
  46. int m_iy;
  47. int m_iv[NTAB];
  48. CThreadFastMutex m_mutex;
  49. };
  50. //-----------------------------------------------------------------------------
  51. // A generator of gaussian distributed random numbers
  52. //-----------------------------------------------------------------------------
  53. class VSTDLIB_CLASS CGaussianRandomStream
  54. {
  55. public:
  56. // Passing in NULL will cause the gaussian stream to use the
  57. // installed global random number generator
  58. CGaussianRandomStream( IUniformRandomStream *pUniformStream = NULL );
  59. // Attaches to a random uniform stream
  60. void AttachToStream( IUniformRandomStream *pUniformStream = NULL );
  61. // Generates random numbers
  62. float RandomFloat( float flMean = 0.0f, float flStdDev = 1.0f );
  63. private:
  64. IUniformRandomStream *m_pUniformStream;
  65. bool m_bHaveValue;
  66. float m_flRandomValue;
  67. CThreadFastMutex m_mutex;
  68. };
  69. //-----------------------------------------------------------------------------
  70. // A couple of convenience functions to access the library's global uniform stream
  71. //-----------------------------------------------------------------------------
  72. VSTDLIB_INTERFACE void RandomSeed( int iSeed );
  73. VSTDLIB_INTERFACE float RandomFloat( float flMinVal = 0.0f, float flMaxVal = 1.0f );
  74. VSTDLIB_INTERFACE float RandomFloatExp( float flMinVal = 0.0f, float flMaxVal = 1.0f, float flExponent = 1.0f );
  75. VSTDLIB_INTERFACE int RandomInt( int iMinVal, int iMaxVal );
  76. VSTDLIB_INTERFACE float RandomGaussianFloat( float flMean = 0.0f, float flStdDev = 1.0f );
  77. //-----------------------------------------------------------------------------
  78. // Installs a global random number generator, which will affect the Random functions above
  79. //-----------------------------------------------------------------------------
  80. VSTDLIB_INTERFACE void InstallUniformRandomStream( IUniformRandomStream *pStream );
  81. #pragma warning(pop)
  82. #endif // VSTDLIB_RANDOM_H