#include "core/utils/Random.hpp" #include constexpr static int M = 7; Core::Random::Random(Seed seed) : data(), index(0) { for(int i = 0; i < data.getLength(); i++) { data[i] = seed; seed = seed * 7 + 31; } } void Core::Random::update() { static const Seed map[2] = {0, 0x8EBFD028}; for(int i = 0; i < data.getLength() - M; i++) { data[i] = data[i + M] ^ (data[i] >> 1) ^ map[data[i] & 1]; } for(int i = data.getLength() - M; i < data.getLength(); i++) { data[i] = data[i + (M - data.getLength())] ^ (data[i] >> 1) ^ map[data[i] & 1]; } index = 0; } int Core::Random::next() { if(index >= data.getLength()) { update(); } Seed r = data[index++]; r ^= (r << 7) & 0x2B5B2500; r ^= (r << 15) & 0xDB8B0000; r ^= (r >> 16); return static_cast(r >> 1); } int Core::Random::next(int min, int inclusiveMax) { return min + next() % (inclusiveMax - min + 1); } bool Core::Random::nextBool() { return next() & 1; } float Core::Random::nextFloat() { static constexpr i32 m = 0x7FFFFFFF; float f = static_cast(next() & m) * (1.0f / static_cast(m)); return f >= 1.0f ? nextFloat() : f; } float Core::Random::nextFloat(float min, float exclusiveMax) { return min + nextFloat() * (exclusiveMax - min); }