panda-core/src/utils/Random.cpp

67 lines
1.3 KiB
C++
Executable File

#include <chrono>
#include <cmath>
#include "utils/Random.hpp"
panda::Random::Random(uint32_t _w, uint32_t _z) {
m_w = _w;
m_z = _z;
}
void panda::Random::setSeed(uint32_t w) {
if (w != 0) m_w = w;
}
void panda::Random::setSeed(uint32_t w, uint32_t z) {
if (w != 0) m_w = w;
if (z != 0) m_z = z;
}
void panda::Random::setSeedFromTime() {
m_w = std::chrono::system_clock::now().time_since_epoch().count();
}
uint32_t panda::Random::MWC() {
m_z = 36969 * (m_z & 65535) + (m_z >> 16);
m_w = 18000 * (m_w & 65535) + (m_w >> 16);
return (m_z << 16) + m_w;
}
float panda::Random::UNI() {
return MWC() * inv_uni;
}
float panda::Random::range(float min, float max) {
return min + (max-min) * UNI();
}
float panda::Random::rangeEx(float min, float max, float lamda) {
const float u = UNI();
return min + (max-min) * std::pow(u, lamda);
}
float panda::Random::xorshift_uni(uint32_t x) {
return xorshift(x) * inv_uni;
}
float panda::Random::xorshift64_uni(uint64_t x) {
return xorshift64(x) * inv_uni64;
}
uint32_t panda::Random::xorshift(uint32_t x) {
x ^= x << 13;
x ^= x >> 17;
x ^= x << 5;
return x;
}
uint64_t panda::Random::xorshift64(uint64_t x) {
x ^= x << 13;
x ^= x >> 7;
x ^= x << 17;
return x;
}