diff --git a/CMakeLists.txt b/CMakeLists.txt index d840ab2..1c73171 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -6,6 +6,7 @@ set(CMAKE_CXX_STANDARD 17) file(GLOB_RECURSE SRC_FILES "src/*.cpp" "src/*.hpp") add_library(Arbutils SHARED ${SRC_FILES}) +set_target_properties(Arbutils PROPERTIES LINKER_LANGUAGE CXX) file(GLOB_RECURSE TEST_FILES "tests/*.cpp" "tests/*.hpp") add_executable(ArbutilsTests ${TEST_FILES} extern/catch.hpp) diff --git a/src/Random.cpp b/src/Random.cpp deleted file mode 100644 index 8c7562a..0000000 --- a/src/Random.cpp +++ /dev/null @@ -1,9 +0,0 @@ -#include "Random.hpp" -#include - -// Seed parameterless constructor with current milliseconds since epoch. -template -Arbutils::BaseRandom::BaseRandom() - : _rng(RandomT( - std::chrono::duration_cast(std::chrono::system_clock::now().time_since_epoch()) - .count())) {} diff --git a/src/Random.hpp b/src/Random.hpp index ace9d2c..7361885 100644 --- a/src/Random.hpp +++ b/src/Random.hpp @@ -3,6 +3,7 @@ #include #include +#include namespace Arbutils { @@ -11,53 +12,57 @@ namespace Arbutils { RandomT _rng; public: - BaseRandom(); - explicit BaseRandom(int32_t seed) : _rng(seed){}; + inline constexpr BaseRandom() + : _rng(RandomT( + std::chrono::duration_cast(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. - [[nodiscard]] inline float GetFloat() { + [[nodiscard]] inline constexpr float GetFloat() { return static_cast(_rng()) / (static_cast(std::mt19937::max() - std::mt19937::min()) - 0.5f); } /// Gets a random double between 0.0 and 1.0. - [[nodiscard]] inline double GetDouble() { + [[nodiscard]] inline constexpr double GetDouble() { return static_cast(_rng()) / ((static_cast(std::mt19937::max()) - std::mt19937::min()) - 0.5); } /// 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(GetDouble() * static_cast(std::numeric_limits::max())); } /// Gets a random 32 bit integer between 0, and given max parameter. /// \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(GetDouble() * static_cast(max)); } /// Gets a random 32 bit integer between given min and max parameters. /// \param min The inclusive min 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(GetDouble() * static_cast(max - min) + min); } /// 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(GetDouble() * static_cast(std::numeric_limits::max())); } /// Gets a random 32 bit unsigned integer between 0, and given max parameter. /// \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(GetDouble() * static_cast(max)); } /// 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 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(GetDouble() * static_cast(max - min) + min); } };