summaryrefslogtreecommitdiff
path: root/src/util/random.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'src/util/random.cpp')
-rw-r--r--src/util/random.cpp37
1 files changed, 37 insertions, 0 deletions
diff --git a/src/util/random.cpp b/src/util/random.cpp
new file mode 100644
index 00000000..ae543765
--- /dev/null
+++ b/src/util/random.cpp
@@ -0,0 +1,37 @@
+/*
+ * Copyright 2023 jacqueline <me@jacqueline.id.au>
+ *
+ * SPDX-License-Identifier: GPL-3.0-only
+ */
+
+#include "random.hpp"
+
+#include <cstdint>
+
+#include "esp_random.h"
+#include "komihash.h"
+
+namespace util {
+
+IRandom* sRandom = new Random();
+
+Random::Random() {
+ esp_fill_random(&seed1_, sizeof(seed1_));
+ seed2_ = seed1_;
+
+ // komirand needs four iterations to properly self-start.
+ for (int i = 0; i < 4; i++) {
+ Next();
+ }
+}
+
+auto Random::Next() -> std::uint64_t {
+ return komirand(&seed1_, &seed2_);
+}
+
+auto Random::RangeInclusive(std::uint64_t lower, std::uint64_t upper)
+ -> std::uint64_t {
+ return (Next() % (upper - lower + 1)) + lower;
+}
+
+} // namespace util