Tweaks for Random class.
continuous-integration/drone/push Build is passing
Details
continuous-integration/drone/push Build is passing
Details
This commit is contained in:
parent
729a1d82f7
commit
5d620e8ee7
|
@ -6,6 +6,7 @@ set(CMAKE_CXX_STANDARD 17)
|
||||||
|
|
||||||
file(GLOB_RECURSE SRC_FILES "src/*.cpp" "src/*.hpp")
|
file(GLOB_RECURSE SRC_FILES "src/*.cpp" "src/*.hpp")
|
||||||
add_library(Arbutils SHARED ${SRC_FILES})
|
add_library(Arbutils SHARED ${SRC_FILES})
|
||||||
|
set_target_properties(Arbutils PROPERTIES LINKER_LANGUAGE CXX)
|
||||||
|
|
||||||
file(GLOB_RECURSE TEST_FILES "tests/*.cpp" "tests/*.hpp")
|
file(GLOB_RECURSE TEST_FILES "tests/*.cpp" "tests/*.hpp")
|
||||||
add_executable(ArbutilsTests ${TEST_FILES} extern/catch.hpp)
|
add_executable(ArbutilsTests ${TEST_FILES} extern/catch.hpp)
|
||||||
|
|
|
@ -1,9 +0,0 @@
|
||||||
#include "Random.hpp"
|
|
||||||
#include <chrono>
|
|
||||||
|
|
||||||
// Seed parameterless constructor with current milliseconds since epoch.
|
|
||||||
template <class RandomT>
|
|
||||||
Arbutils::BaseRandom<RandomT>::BaseRandom()
|
|
||||||
: _rng(RandomT(
|
|
||||||
std::chrono::duration_cast<std::chrono::milliseconds>(std::chrono::system_clock::now().time_since_epoch())
|
|
||||||
.count())) {}
|
|
|
@ -3,6 +3,7 @@
|
||||||
|
|
||||||
#include <cstdint>
|
#include <cstdint>
|
||||||
#include <random>
|
#include <random>
|
||||||
|
#include <chrono>
|
||||||
|
|
||||||
namespace Arbutils {
|
namespace Arbutils {
|
||||||
|
|
||||||
|
@ -11,53 +12,57 @@ namespace Arbutils {
|
||||||
RandomT _rng;
|
RandomT _rng;
|
||||||
|
|
||||||
public:
|
public:
|
||||||
BaseRandom();
|
inline constexpr BaseRandom()
|
||||||
explicit BaseRandom(int32_t seed) : _rng(seed){};
|
: _rng(RandomT(
|
||||||
|
std::chrono::duration_cast<std::chrono::milliseconds>(std::chrono::system_clock::now().time_since_epoch())
|
||||||
|
.count())) {}
|
||||||
|
|
||||||
|
explicit inline constexpr BaseRandom(int32_t seed) : _rng(seed){};
|
||||||
|
|
||||||
/// Gets a random float between 0.0 and 1.0.
|
/// Gets a random float between 0.0 and 1.0.
|
||||||
[[nodiscard]] inline float GetFloat() {
|
[[nodiscard]] inline constexpr float GetFloat() {
|
||||||
return static_cast<float>(_rng()) / (static_cast<float>(std::mt19937::max() - std::mt19937::min()) - 0.5f);
|
return static_cast<float>(_rng()) / (static_cast<float>(std::mt19937::max() - std::mt19937::min()) - 0.5f);
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Gets a random double between 0.0 and 1.0.
|
/// Gets a random double between 0.0 and 1.0.
|
||||||
[[nodiscard]] inline double GetDouble() {
|
[[nodiscard]] inline constexpr double GetDouble() {
|
||||||
return static_cast<double>(_rng()) /
|
return static_cast<double>(_rng()) /
|
||||||
((static_cast<double>(std::mt19937::max()) - std::mt19937::min()) - 0.5);
|
((static_cast<double>(std::mt19937::max()) - std::mt19937::min()) - 0.5);
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Gets a random 32 bit integer between 0 and max int.
|
/// Gets a random 32 bit integer between 0 and max int.
|
||||||
[[nodiscard]] inline int32_t Get() {
|
[[nodiscard]] inline constexpr int32_t Get() {
|
||||||
return static_cast<int32_t>(GetDouble() * static_cast<float>(std::numeric_limits<int32_t>::max()));
|
return static_cast<int32_t>(GetDouble() * static_cast<float>(std::numeric_limits<int32_t>::max()));
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Gets a random 32 bit integer between 0, and given max parameter.
|
/// Gets a random 32 bit integer between 0, and given max parameter.
|
||||||
/// \param max The exclusive max value the random value should be.
|
/// \param max The exclusive max value the random value should be.
|
||||||
[[nodiscard]] inline int32_t Get(int32_t max) {
|
[[nodiscard]] inline constexpr int32_t Get(int32_t max) {
|
||||||
return static_cast<int32_t>(GetDouble() * static_cast<float>(max));
|
return static_cast<int32_t>(GetDouble() * static_cast<float>(max));
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Gets a random 32 bit integer between given min and max parameters.
|
/// Gets a random 32 bit integer between given min and max parameters.
|
||||||
/// \param min The inclusive min value the random value should be.
|
/// \param min The inclusive min value the random value should be.
|
||||||
/// \param max The exclusive max value the random value should be.
|
/// \param max The exclusive max value the random value should be.
|
||||||
[[nodiscard]] inline int32_t Get(int32_t min, int32_t max) {
|
[[nodiscard]] inline constexpr int32_t Get(int32_t min, int32_t max) {
|
||||||
return static_cast<int32_t>(GetDouble() * static_cast<float>(max - min) + min);
|
return static_cast<int32_t>(GetDouble() * static_cast<float>(max - min) + min);
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Gets a random 32 bit unsigned integer between 0 and max unsigned int.
|
/// Gets a random 32 bit unsigned integer between 0 and max unsigned int.
|
||||||
[[nodiscard]] inline uint32_t GetUnsigned() {
|
[[nodiscard]] inline constexpr uint32_t GetUnsigned() {
|
||||||
return static_cast<int32_t>(GetDouble() * static_cast<float>(std::numeric_limits<uint32_t>::max()));
|
return static_cast<int32_t>(GetDouble() * static_cast<float>(std::numeric_limits<uint32_t>::max()));
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Gets a random 32 bit unsigned integer between 0, and given max parameter.
|
/// Gets a random 32 bit unsigned integer between 0, and given max parameter.
|
||||||
/// \param max The exclusive max value the random value should be.
|
/// \param max The exclusive max value the random value should be.
|
||||||
[[nodiscard]] inline uint32_t GetUnsigned(uint32_t max) {
|
[[nodiscard]] inline constexpr uint32_t GetUnsigned(uint32_t max) {
|
||||||
return static_cast<int32_t>(GetDouble() * static_cast<float>(max));
|
return static_cast<int32_t>(GetDouble() * static_cast<float>(max));
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Gets a random 32 bit unsigned integer between given min and max parameters.
|
/// Gets a random 32 bit unsigned integer between given min and max parameters.
|
||||||
/// \param min The inclusive min value the random value should be.
|
/// \param min The inclusive min value the random value should be.
|
||||||
/// \param max The exclusive max value the random value should be.
|
/// \param max The exclusive max value the random value should be.
|
||||||
[[nodiscard]] inline uint32_t GetUnsigned(uint32_t min, uint32_t max) {
|
[[nodiscard]] inline constexpr uint32_t GetUnsigned(uint32_t min, uint32_t max) {
|
||||||
return static_cast<int32_t>(GetDouble() * static_cast<float>(max - min) + min);
|
return static_cast<int32_t>(GetDouble() * static_cast<float>(max - min) + min);
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
Loading…
Reference in New Issue