1
0
mirror of https://github.com/alliedmodders/hl2sdk.git synced 2025-01-09 10:39:03 +08:00
hl2sdk/public/vstdlib/random.h
Nick Hastings f60592b4f9 More Source 2 / Dota shuffling.
- Remove tier1 interface/factory funcs that no longer exist.
- Add/fix tier0 interface funcs.
- Fix tier0 win64 link lib to have correct decorated names for Msg, Warning, etc.
- Update Error() shim to act more closely to old behavior.
- Moved CreateInterface impl to interfaces lib (appears to not be in tier1 anymore).
- Removed exports log channels that aren't exported in S2.
2018-06-30 08:15:58 -04:00

114 lines
3.7 KiB
C++

//===== Copyright © 1996-2005, Valve Corporation, All rights reserved. ======//
//
// Purpose: Random number generator
//
// $Workfile: $
// $NoKeywords: $
//===========================================================================//
#ifndef VSTDLIB_RANDOM_H
#define VSTDLIB_RANDOM_H
#include "vstdlib/vstdlib.h"
#include "tier0/basetypes.h"
#include "tier0/threadtools.h"
#define NTAB 32
#ifdef _MSC_VER
#pragma warning(push)
#pragma warning( disable:4251 )
#endif
//-----------------------------------------------------------------------------
// A generator of uniformly distributed random numbers
//-----------------------------------------------------------------------------
class IUniformRandomStream
{
public:
// Sets the seed of the random number generator
virtual void SetSeed( int iSeed ) = 0;
// Generates random numbers
virtual float RandomFloat( float flMinVal = 0.0f, float flMaxVal = 1.0f ) = 0;
virtual int RandomInt( int iMinVal, int iMaxVal ) = 0;
virtual float RandomFloatExp( float flMinVal = 0.0f, float flMaxVal = 1.0f, float flExponent = 1.0f ) = 0;
};
//-----------------------------------------------------------------------------
// The standard generator of uniformly distributed random numbers
//-----------------------------------------------------------------------------
class VSTDLIB_CLASS CUniformRandomStream : public IUniformRandomStream
{
public:
CUniformRandomStream();
// Sets the seed of the random number generator
virtual void SetSeed( int iSeed );
// Generates random numbers
virtual float RandomFloat( float flMinVal = 0.0f, float flMaxVal = 1.0f );
virtual int RandomInt( int iMinVal, int iMaxVal );
virtual float RandomFloatExp( float flMinVal = 0.0f, float flMaxVal = 1.0f, float flExponent = 1.0f );
private:
int GenerateRandomNumber();
int m_idum;
int m_iy;
int m_iv[NTAB];
CThreadFastMutex m_mutex;
};
//-----------------------------------------------------------------------------
// A generator of gaussian distributed random numbers
//-----------------------------------------------------------------------------
class VSTDLIB_CLASS CGaussianRandomStream
{
public:
// Passing in NULL will cause the gaussian stream to use the
// installed global random number generator
CGaussianRandomStream( IUniformRandomStream *pUniformStream = NULL );
// Attaches to a random uniform stream
void AttachToStream( IUniformRandomStream *pUniformStream = NULL );
// Generates random numbers
float RandomFloat( float flMean = 0.0f, float flStdDev = 1.0f );
private:
IUniformRandomStream *m_pUniformStream;
bool m_bHaveValue;
float m_flRandomValue;
CThreadFastMutex m_mutex;
};
//-----------------------------------------------------------------------------
// A couple of convenience functions to access the library's global uniform stream
//-----------------------------------------------------------------------------
VSTDLIB_INTERFACE void RandomSeed( int iSeed );
VSTDLIB_INTERFACE float RandomFloat( float flMinVal = 0.0f, float flMaxVal = 1.0f );
VSTDLIB_INTERFACE float RandomFloatExp( float flMinVal = 0.0f, float flMaxVal = 1.0f, float flExponent = 1.0f );
VSTDLIB_INTERFACE int RandomInt( int iMinVal, int iMaxVal );
VSTDLIB_INTERFACE float RandomGaussianFloat( float flMean = 0.0f, float flStdDev = 1.0f );
//-----------------------------------------------------------------------------
// Installs a global random number generator, which will affect the Random functions above
//-----------------------------------------------------------------------------
VSTDLIB_INTERFACE void InstallUniformRandomStream( IUniformRandomStream *pStream );
#ifdef _MSC_VER
#pragma warning(pop)
#endif
#endif // VSTDLIB_RANDOM_H