diff --git a/soh/soh/Enhancements/randomizer/3drando/random.cpp b/soh/soh/Enhancements/randomizer/3drando/random.cpp index 92cf22b85..e2717ef75 100644 --- a/soh/soh/Enhancements/randomizer/3drando/random.cpp +++ b/soh/soh/Enhancements/randomizer/3drando/random.cpp @@ -1,36 +1,55 @@ #include "random.hpp" +#include #include -#include -#include -#include +#include static bool init = false; -static boost::random::mt19937 generator; +static uint64_t state = 0; +const uint64_t multiplier = 6364136223846793005ULL; +const uint64_t increment = 11634580027462260723ULL; //Initialize with seed specified -void Random_Init(uint32_t seed) { +void Random_Init(uint64_t seed) { init = true; - generator = boost::random::mt19937{seed}; + state = seed; +} + +uint32_t next32() { + if (!init) { + //No seed given, get a random number from device to seed +#if !defined(__SWITCH__) && !defined(__WIIU__) + uint64_t seed = static_cast(std::random_device{}()); +#else + uint64_t seed = static_cast(std::hash{}(std::to_string(rand()))); +#endif + Random_Init(seed); + } + + state = state * multiplier + increment; + uint32_t xorshifted = static_cast(((state >> 18) ^ state) >> 27); + uint32_t rot = static_cast(state >> 59); + return std::rotr(xorshifted, rot); } //Returns a random integer in range [min, max-1] uint32_t Random(int min, int max) { - if (!init) { - //No seed given, get a random number from device to seed -#if !defined(__SWITCH__) && !defined(__WIIU__) - const auto seed = static_cast(std::random_device{}()); -#else - uint32_t seed = static_cast(std::hash{}(std::to_string(rand()))); -#endif - Random_Init(seed); + if (min == max) { + return min; + } + assert(max > min); + + uint32_t n = max - min; + uint32_t cutoff = UINT32_MAX - UINT32_MAX % static_cast(n); + for (;;) { + uint32_t r = next32(); + if (r <= cutoff) { + return min + r % n; + } } - boost::random::uniform_int_distribution distribution(min, max-1); - return distribution(generator); } //Returns a random floating point number in [0.0, 1.0] double RandomDouble() { - boost::random::uniform_real_distribution distribution(0.0, 1.0); - return distribution(generator); + return ldexp(next32(), -32); } diff --git a/soh/soh/Enhancements/randomizer/3drando/random.hpp b/soh/soh/Enhancements/randomizer/3drando/random.hpp index 24ab78dcd..7151d364b 100644 --- a/soh/soh/Enhancements/randomizer/3drando/random.hpp +++ b/soh/soh/Enhancements/randomizer/3drando/random.hpp @@ -7,7 +7,7 @@ #include #include -void Random_Init(uint32_t seed); +void Random_Init(uint64_t seed); uint32_t Random(int min, int max); double RandomDouble();