Shipwright/soh/soh/Enhancements/randomizer/3drando/random.cpp
Malkierian 45d9fe54d2
Warnings Squashing Round 1 (#5469)
* Fix truncation warning in `entrance.h`.

* Fix type warning in fishsanity.cpp

* Fix implicit conversion warnings in BossRush, Mouse, and UIWidgets.hpp.

* Add Random_Float and use it in GameInteractor_RawAction.cpp.
Make non-specified seed init a separate function.

* clang after type change

* Fix truncation and conversion warnings in ExtraTraps.cpp.

* Resolve type conversion warnings in InputViewer.cpp.

* Resolve some type conversion warnings in hook_handlers.cpp.

* Remove `Random_Float`, and apply `RandomDouble` where it was being used instead.
Add proper range notation to `RandomDouble` to denote the lack of inclusivity for the max part of the range.

* Convert c-style casts to static_cast.
2025-05-13 19:30:04 -07:00

40 lines
1.2 KiB
C++

#include "random.hpp"
#include <random>
#include <boost/random/mersenne_twister.hpp>
#include <boost/random/uniform_int_distribution.hpp>
#include <boost/random/uniform_real_distribution.hpp>
static bool init = false;
static boost::random::mt19937 generator;
// Initialize with seed specified
void Random_Init(uint32_t seed) {
init = true;
generator = boost::random::mt19937{ seed };
}
void Random_InitSeed() {
if (!init) {
// No seed given, get a random number from device to seed
#if !defined(__SWITCH__) && !defined(__WIIU__)
const auto seed = static_cast<uint32_t>(std::random_device{}());
#else
uint32_t seed = static_cast<uint32_t>(std::hash<std::string>{}(std::to_string(rand())));
#endif
Random_Init(seed);
}
}
// Returns a random unsigned integer in range [min, max-1]
uint32_t Random(uint32_t min, uint32_t max) {
Random_InitSeed();
boost::random::uniform_int_distribution<uint32_t> 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<double> distribution(0.0, 1.0);
return distribution(generator);
}