Counter Strike : Global Offensive Source Code
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.

116 lines
3.7 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. #if !defined( __SPU__ )
  11. #include "vstdlib/vstdlib.h"
  12. #include "tier0/basetypes.h"
  13. #include "tier0/threadtools.h"
  14. #include "tier1/interface.h"
  15. #define NTAB 32
  16. #pragma warning(push)
  17. #pragma warning( disable:4251 )
  18. //-----------------------------------------------------------------------------
  19. // A generator of uniformly distributed random numbers
  20. //-----------------------------------------------------------------------------
  21. class IUniformRandomStream
  22. {
  23. public:
  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. // Installs a global random number generator, which will affect the Random functions above
  80. //-----------------------------------------------------------------------------
  81. VSTDLIB_INTERFACE void InstallUniformRandomStream( IUniformRandomStream *pStream );
  82. #pragma warning(pop)
  83. #endif // #if !defined( __SPU__ )
  84. #endif // VSTDLIB_RANDOM_H