project64/Source/Common/Random.cpp

45 lines
681 B
C++
Raw Normal View History

/*
* Implements the CRandom class.
*
* This class implements the Lehmer Random Number Generator.
*
*/
#include "stdafx.h"
#include "Random.h"
#include <time.h>
CRandom::CRandom()
{
2017-10-18 05:16:30 +00:00
m_state = (uint32_t)time(NULL);
}
CRandom::CRandom(uint32_t state_value)
{
2017-10-18 05:16:30 +00:00
m_state = state_value;
}
uint32_t CRandom::randomizer(uint32_t val)
{
return ((uint64_t)val * 279470273UL) % 4294967291UL;
}
uint32_t CRandom::next()
{
2017-10-18 05:16:30 +00:00
m_state = randomizer(m_state);
return m_state;
}
void CRandom::set_state(uint32_t state_value)
{
if (state_value == 0)
2017-10-18 05:16:30 +00:00
m_state = 1;
else
2017-10-18 05:16:30 +00:00
m_state = state_value;
}
uint32_t CRandom::get_state()
{
2017-10-18 05:16:30 +00:00
return m_state;
}