Random number: Difference between revisions
Jump to navigation
Jump to search
TamaHobbit (talk | contribs) (Added explanation of IUniformRandomStream, since I noticed a quirk to C++ programmers) |
TamaHobbit (talk | contribs) |
||
Line 7: | Line 7: | ||
== IUniformRandomStream == | == IUniformRandomStream == | ||
//----------------------------------------------------------------------------- | //----------------------------------------------------------------------------- | ||
// A generator of uniformly distributed random numbers | // A generator of uniformly distributed random numbers | ||
//----------------------------------------------------------------------------- | //----------------------------------------------------------------------------- | ||
class IUniformRandomStream | class IUniformRandomStream | ||
{ | { | ||
public: | 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; | |||
}; | }; | ||
This is a random generator included in the source SDK, which allows you to generate random numbers, for instance to choose a random spawn point within an area: | This is a random generator included in the source SDK, which allows you to generate random numbers, for instance to choose a random spawn point within an area: | ||
CEmpSpawn *picked = candidates[random->RandomInt(0, candidates.Count() - 1)]; | CEmpSpawn *picked = candidates[random->RandomInt(0, candidates.Count() - 1)]; | ||
Note the - 1; | random is a global set up in the initialization of the source SDK. Note the - 1; RandomInt(0,1) includes the upperbound, unlike its counterpart in the C++ stdlib and boost libraries. This upper bound behaviour can be readily tested by putting this anywhere you know will run: | ||
for (int i = 0; i < 50000; ++i) { | for (int i = 0; i < 50000; ++i) { | ||
Assert(random->RandomInt(0, 2) < 2); | Assert(random->RandomInt(0, 2) < 2); | ||
} | } |
Revision as of 07:21, 31 December 2015
There is no built-in entity to generate random numbers in Valve's Source games.
However, you can use logic_case's PickRandom
input to trigger one of up to sixteen random outputs.
IUniformRandomStream
//----------------------------------------------------------------------------- // 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; };
This is a random generator included in the source SDK, which allows you to generate random numbers, for instance to choose a random spawn point within an area:
CEmpSpawn *picked = candidates[random->RandomInt(0, candidates.Count() - 1)];
random is a global set up in the initialization of the source SDK. Note the - 1; RandomInt(0,1) includes the upperbound, unlike its counterpart in the C++ stdlib and boost libraries. This upper bound behaviour can be readily tested by putting this anywhere you know will run:
for (int i = 0; i < 50000; ++i) {
Assert(random->RandomInt(0, 2) < 2);
}