summaryrefslogtreecommitdiff
path: root/src/util/random.cpp
diff options
context:
space:
mode:
authorjacqueline <me@jacqueline.id.au>2023-07-17 16:54:35 +1000
committerjacqueline <me@jacqueline.id.au>2023-07-17 16:54:35 +1000
commit7197da21f6bcc1aaa5d1905228e0e2ec1caf3fa8 (patch)
treef24f81cba08160d45d7e994dc31f48506e823e49 /src/util/random.cpp
parentb6bc6b9e47605ede9bffe50445d1afe3acf0ab49 (diff)
downloadtangara-fw-7197da21f6bcc1aaa5d1905228e0e2ec1caf3fa8.tar.gz
Basic playlists for upcoming
Beware under-testing and bugs. Just getting something barebones in so that I can do rN+1 bringup
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