summaryrefslogtreecommitdiffstats
path: root/core/math/randomizer.hh
diff options
context:
space:
mode:
authoruntodesu <kirill@untode.su>2025-07-01 03:08:39 +0500
committeruntodesu <kirill@untode.su>2025-07-01 03:08:39 +0500
commit458e0005690ea9d579588a0a12368fc2c2c9a93a (patch)
tree588a9ca6cb3c76d9193b5bd4601d64f0e50e8c8c /core/math/randomizer.hh
parentc7b0c8e0286a1b2bb7ec55e579137dfc3b22eeb9 (diff)
downloadvoxelius-458e0005690ea9d579588a0a12368fc2c2c9a93a.tar.bz2
voxelius-458e0005690ea9d579588a0a12368fc2c2c9a93a.zip
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
Diffstat (limited to 'core/math/randomizer.hh')
-rw-r--r--core/math/randomizer.hh60
1 files changed, 60 insertions, 0 deletions
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<typename T>
+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<T> m_vector;
+ std::mt19937_64 m_twister;
+ std::uniform_int_distribution<std::size_t> m_dist;
+};
+} // namespace math
+
+template<typename T>
+inline math::Randomizer<T>::Randomizer(void)
+{
+ m_vector.clear();
+ m_twister.seed(std::random_device()());
+ m_dist = std::uniform_int_distribution<std::size_t>(0, 0);
+}
+
+template<typename T>
+inline math::Randomizer<T>::Randomizer(std::uint64_t seed)
+{
+ m_vector.clear();
+ m_twister.seed(seed);
+ m_dist = std::uniform_int_distribution<std::size_t>(0, 0);
+}
+
+template<typename T>
+inline void math::Randomizer<T>::add(const T& value)
+{
+ m_vector.push_back(value);
+ m_dist = std::uniform_int_distribution<std::size_t>(0, m_vector.size() - 1);
+}
+
+template<typename T>
+inline const T& math::Randomizer<T>::get(void)
+{
+ return m_vector.at(m_dist(m_twister));
+}
+
+template<typename T>
+inline void math::Randomizer<T>::clear(void)
+{
+ m_vector.clear();
+ m_dist = std::uniform_int_distribution<std::size_t>(0, 0);
+}
+
+#endif // CORE_MATH_RANDOMIZER_HH