From 458e0005690ea9d579588a0a12368fc2c2c9a93a Mon Sep 17 00:00:00 2001 From: untodesu Date: Tue, 1 Jul 2025 03:08:39 +0500 Subject: I hyper-focued on refactoring again - I put a cool-sounding "we are number one" remix on repeat and straight up grinded the entire repository to a better state until 03:09 AM. I guess I have something wrong in my brain that makes me do this shit --- core/math/randomizer.hh | 60 +++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 60 insertions(+) create mode 100644 core/math/randomizer.hh (limited to 'core/math/randomizer.hh') diff --git a/core/math/randomizer.hh b/core/math/randomizer.hh new file mode 100644 index 0000000..206af42 --- /dev/null +++ b/core/math/randomizer.hh @@ -0,0 +1,60 @@ +#ifndef CORE_MATH_RANDOMIZER_HH +#define CORE_MATH_RANDOMIZER_HH 1 +#pragma once + +namespace math +{ +template +class Randomizer final { +public: + explicit Randomizer(void); + explicit Randomizer(std::uint64_t seed); + virtual ~Randomizer(void) = default; + void add(const T& value); + const T& get(void); + void clear(void); + +private: + std::vector m_vector; + std::mt19937_64 m_twister; + std::uniform_int_distribution m_dist; +}; +} // namespace math + +template +inline math::Randomizer::Randomizer(void) +{ + m_vector.clear(); + m_twister.seed(std::random_device()()); + m_dist = std::uniform_int_distribution(0, 0); +} + +template +inline math::Randomizer::Randomizer(std::uint64_t seed) +{ + m_vector.clear(); + m_twister.seed(seed); + m_dist = std::uniform_int_distribution(0, 0); +} + +template +inline void math::Randomizer::add(const T& value) +{ + m_vector.push_back(value); + m_dist = std::uniform_int_distribution(0, m_vector.size() - 1); +} + +template +inline const T& math::Randomizer::get(void) +{ + return m_vector.at(m_dist(m_twister)); +} + +template +inline void math::Randomizer::clear(void) +{ + m_vector.clear(); + m_dist = std::uniform_int_distribution(0, 0); +} + +#endif // CORE_MATH_RANDOMIZER_HH -- cgit