Add Random_Float and use it in GameInteractor_RawAction.cpp.

Make non-specified seed init a separate function.
This commit is contained in:
Malkierian 2025-05-03 19:27:48 -07:00
commit 705b32e347
3 changed files with 24 additions and 12 deletions

View file

@ -617,7 +617,7 @@ GameInteractionEffectQueryResult GameInteractor::RawAction::SpawnEnemyWithOffset
} }
// Generate point in random angle with a radius. // Generate point in random angle with a radius.
float angle = Random(0, 2 * M_PI); float angle = Random_Float(0, 2 * M_PI);
float radius = 150; float radius = 150;
float posXOffset = radius * cos(angle); float posXOffset = radius * cos(angle);
float posZOffset = radius * sin(angle); float posZOffset = radius * sin(angle);

View file

@ -14,8 +14,7 @@ void Random_Init(uint32_t seed) {
generator = boost::random::mt19937{ seed }; generator = boost::random::mt19937{ seed };
} }
// Returns a random integer in range [min, max-1] void Random_InitSeed() {
uint32_t Random(int min, int max) {
if (!init) { if (!init) {
// No seed given, get a random number from device to seed // No seed given, get a random number from device to seed
#if !defined(__SWITCH__) && !defined(__WIIU__) #if !defined(__SWITCH__) && !defined(__WIIU__)
@ -25,10 +24,22 @@ uint32_t Random(int min, int max) {
#endif #endif
Random_Init(seed); 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); boost::random::uniform_int_distribution<uint32_t> distribution(min, max - 1);
return distribution(generator); return distribution(generator);
} }
// Returns a random float in range [min, max-1]
float Random_Float(float min, float max) {
Random_InitSeed();
boost::random::uniform_real_distribution<float> distribution(min, max - 1);
return distribution(generator);
}
// Returns a random floating point number in [0.0, 1.0] // Returns a random floating point number in [0.0, 1.0]
double RandomDouble() { double RandomDouble() {
boost::random::uniform_real_distribution<double> distribution(0.0, 1.0); boost::random::uniform_real_distribution<double> distribution(0.0, 1.0);

View file

@ -8,7 +8,8 @@
#include <set> #include <set>
void Random_Init(uint32_t seed); void Random_Init(uint32_t seed);
uint32_t Random(int min, int max); uint32_t Random(uint32_t min, uint32_t max);
float Random_Float(float min, float max);
double RandomDouble(); double RandomDouble();
// Get a random element from a vector or array // Get a random element from a vector or array
@ -21,17 +22,17 @@ template <typename T> T RandomElement(std::vector<T>& vector, bool erase) {
return selected; return selected;
} }
template <typename Container> auto& RandomElement(Container& container) { template <typename Container> auto& RandomElement(Container& container) {
return container[Random(0, std::size(container))]; return container[Random(0, (uint32_t)std::size(container))];
} }
template <typename Container> const auto& RandomElement(const Container& container) { template <typename Container> const auto& RandomElement(const Container& container) {
return container[Random(0, std::size(container))]; return container[Random(0, (uint32_t)std::size(container))];
} }
template <typename T> const T RandomElementFromSet(const std::set<T>& set) { template <typename T> const T RandomElementFromSet(const std::set<T>& set) {
if (set.size() == 1) { if (set.size() == 1) {
return *set.begin(); return *set.begin();
} }
uint32_t rand = Random(0, set.size()); uint32_t rand = Random(0, (uint32_t)set.size());
auto it = set.begin(); auto it = set.begin();
for (uint32_t i = 0; i < rand; i++) { for (uint32_t i = 0; i < rand; i++) {
it++; it++;
@ -43,12 +44,12 @@ template <typename T> const T RandomElementFromSet(const std::set<T>& set) {
// Shuffle items within a vector or array // Shuffle items within a vector or array
// RANDOTODO There's probably a more efficient way to do what this does. // RANDOTODO There's probably a more efficient way to do what this does.
template <typename T> void Shuffle(std::vector<T>& vector) { template <typename T> void Shuffle(std::vector<T>& vector) {
for (std::size_t i = 0; i + 1 < vector.size(); i++) { for (size_t i = 0; i + 1 < vector.size(); i++) {
std::swap(vector[i], vector[Random(i, vector.size())]); std::swap(vector[i], vector[Random((uint32_t)i, (uint32_t)vector.size())]);
} }
} }
template <typename T, std::size_t size> void Shuffle(std::array<T, size>& arr) { template <typename T, size_t size> void Shuffle(std::array<T, size>& arr) {
for (std::size_t i = 0; i + 1 < arr.size(); i++) { for (size_t i = 0; i + 1 < arr.size(); i++) {
std::swap(arr[i], arr[Random(i, arr.size())]); std::swap(arr[i], arr[Random((uint32_t)i, (uint32_t)arr.size())]);
} }
} }