mirror of
https://github.com/HarbourMasters/Shipwright.git
synced 2025-08-22 22:33:43 -07:00
Add Random_Float and use it in GameInteractor_RawAction.cpp.
Make non-specified seed init a separate function.
This commit is contained in:
parent
7a745a4b97
commit
705b32e347
3 changed files with 24 additions and 12 deletions
|
@ -617,7 +617,7 @@ GameInteractionEffectQueryResult GameInteractor::RawAction::SpawnEnemyWithOffset
|
|||
}
|
||||
|
||||
// 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 posXOffset = radius * cos(angle);
|
||||
float posZOffset = radius * sin(angle);
|
||||
|
|
|
@ -14,8 +14,7 @@ void Random_Init(uint32_t seed) {
|
|||
generator = boost::random::mt19937{ seed };
|
||||
}
|
||||
|
||||
// Returns a random integer in range [min, max-1]
|
||||
uint32_t Random(int min, int max) {
|
||||
void Random_InitSeed() {
|
||||
if (!init) {
|
||||
// No seed given, get a random number from device to seed
|
||||
#if !defined(__SWITCH__) && !defined(__WIIU__)
|
||||
|
@ -25,10 +24,22 @@ uint32_t Random(int min, int max) {
|
|||
#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 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]
|
||||
double RandomDouble() {
|
||||
boost::random::uniform_real_distribution<double> distribution(0.0, 1.0);
|
||||
|
|
|
@ -8,7 +8,8 @@
|
|||
#include <set>
|
||||
|
||||
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();
|
||||
|
||||
// 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;
|
||||
}
|
||||
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) {
|
||||
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) {
|
||||
if (set.size() == 1) {
|
||||
return *set.begin();
|
||||
}
|
||||
uint32_t rand = Random(0, set.size());
|
||||
uint32_t rand = Random(0, (uint32_t)set.size());
|
||||
auto it = set.begin();
|
||||
for (uint32_t i = 0; i < rand; i++) {
|
||||
it++;
|
||||
|
@ -43,12 +44,12 @@ template <typename T> const T RandomElementFromSet(const std::set<T>& set) {
|
|||
// Shuffle items within a vector or array
|
||||
// RANDOTODO There's probably a more efficient way to do what this does.
|
||||
template <typename T> void Shuffle(std::vector<T>& vector) {
|
||||
for (std::size_t i = 0; i + 1 < vector.size(); i++) {
|
||||
std::swap(vector[i], vector[Random(i, vector.size())]);
|
||||
for (size_t i = 0; i + 1 < vector.size(); i++) {
|
||||
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) {
|
||||
for (std::size_t i = 0; i + 1 < arr.size(); i++) {
|
||||
std::swap(arr[i], arr[Random(i, arr.size())]);
|
||||
template <typename T, size_t size> void Shuffle(std::array<T, size>& arr) {
|
||||
for (size_t i = 0; i + 1 < arr.size(); i++) {
|
||||
std::swap(arr[i], arr[Random((uint32_t)i, (uint32_t)arr.size())]);
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue