2021-03-16 22:54:15 +00:00
|
|
|
// Implements the CRandom class
|
|
|
|
// This class implements the Lehmer Random Number Generator
|
2017-10-18 02:38:38 +00:00
|
|
|
|
|
|
|
#include "Random.h"
|
|
|
|
#include <time.h>
|
|
|
|
|
|
|
|
CRandom::CRandom()
|
|
|
|
{
|
2021-04-12 11:35:39 +00:00
|
|
|
m_state = (uint32_t)time(nullptr);
|
2017-10-18 02:38:38 +00:00
|
|
|
}
|
|
|
|
|
2017-10-18 03:52:07 +00:00
|
|
|
CRandom::CRandom(uint32_t state_value)
|
2017-10-18 02:38:38 +00:00
|
|
|
{
|
2017-10-18 05:16:30 +00:00
|
|
|
m_state = state_value;
|
2017-10-18 02:38:38 +00:00
|
|
|
}
|
|
|
|
|
2017-10-18 03:52:07 +00:00
|
|
|
uint32_t CRandom::randomizer(uint32_t val)
|
2017-10-18 02:38:38 +00:00
|
|
|
{
|
|
|
|
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;
|
2017-10-18 02:38:38 +00:00
|
|
|
}
|
|
|
|
|
2017-10-18 03:52:07 +00:00
|
|
|
void CRandom::set_state(uint32_t state_value)
|
2017-10-18 02:38:38 +00:00
|
|
|
{
|
2022-07-11 10:00:25 +00:00
|
|
|
m_state = state_value == 0 ? 1 : state_value;
|
2017-10-18 03:52:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
uint32_t CRandom::get_state()
|
|
|
|
{
|
2017-10-18 05:16:30 +00:00
|
|
|
return m_state;
|
2017-10-18 02:38:38 +00:00
|
|
|
}
|