1
0
forked from 0ad/0ad

Modified CSimulation's random number generator to use the Boost small_int and uniform_01 distributions for better portability.

This was SVN commit r4203.
This commit is contained in:
Matei 2006-08-07 20:38:23 +00:00
parent 3e20982866
commit d789c92bd0
2 changed files with 21 additions and 11 deletions

View File

@ -35,13 +35,12 @@ using namespace std;
extern CConsole *g_Console;
CSimulation::CSimulation(CGame *pGame, uint randomSeed):
CSimulation::CSimulation(CGame *pGame):
m_pGame(pGame),
m_pWorld(pGame->GetWorld()),
m_pTurnManager((g_SinglePlayerTurnManager=new CSinglePlayerTurnManager())),
m_DeltaTime(0)
{
m_Random.seed((unsigned long) randomSeed);
}
CSimulation::~CSimulation()
@ -52,6 +51,8 @@ CSimulation::~CSimulation()
int CSimulation::Initialize(CGameAttributes* pAttribs)
{
m_Random.seed(0); // TODO: Store a random seed in CGameAttributes and synchronize it accross the network
m_pTurnManager->Initialize(m_pGame->GetNumPlayers());
// Call the game startup script
@ -389,3 +390,18 @@ void CSimulation::QueueLocalCommand(CNetMessage *pMsg)
{
m_pTurnManager->QueueLocalCommand(pMsg);
}
// Get a random integer between 0 and maxVal-1 from the simulation's random number generator
int CSimulation::RandInt(int maxVal)
{
boost::uniform_smallint<int> distr(0, maxVal-1);
return distr(m_Random);
}
// Get a random float in [0, 1) from the simulation's random number generator
float CSimulation::RandFloat()
{
boost::uniform_01<boost::mt19937, float> distr(m_Random);
return distr();
}

View File

@ -29,7 +29,7 @@ class CSimulation
void Interpolate(double frameTime, double offset);
public:
CSimulation(CGame *pGame, uint randomSeed=0);
CSimulation(CGame *pGame);
~CSimulation();
inline void SetTurnManager(CTurnManager *pTurnManager)
@ -55,16 +55,10 @@ public:
void QueueLocalCommand(CNetMessage *pMsg);
// Get a random integer between 0 and maxVal-1 from the simulation's random number generator
int RandInt(int maxVal)
{
return m_Random() % maxVal;
}
int RandInt(int maxVal);
// Get a random float in [0, 1) from the simulation's random number generator
float RandFloat()
{
return float(m_Random()) * (1.0f/4294967296.0f);
}
float RandFloat();
};
#endif