summary refs log tree commit diff
path: root/src/Math/Random.cpp
diff options
context:
space:
mode:
authorMel <einebeere@gmail.com>2023-07-07 21:39:42 +0200
committerMel <einebeere@gmail.com>2023-07-07 21:39:42 +0200
commitf1fc192ddc4c739fa8b4b376c759b7d3218a34eb (patch)
tree9e9afb9a21ba3ca27d1f25d46230aa9d27f8be39 /src/Math/Random.cpp
parent24b8124469350d1c80d0553cf3f4bf58cdb1489b (diff)
downloadmeowcraft-f1fc192ddc4c739fa8b4b376c759b7d3218a34eb.tar.zst
meowcraft-f1fc192ddc4c739fa8b4b376c759b7d3218a34eb.zip
Chunk-bound tree decoration
Diffstat (limited to 'src/Math/Random.cpp')
-rw-r--r--src/Math/Random.cpp31
1 files changed, 31 insertions, 0 deletions
diff --git a/src/Math/Random.cpp b/src/Math/Random.cpp
new file mode 100644
index 0000000..e35cda7
--- /dev/null
+++ b/src/Math/Random.cpp
@@ -0,0 +1,31 @@
+#include "Random.hpp"
+
+namespace Math::Random {
+
+std::array<uint8_t, 4> break_float(const float f) {
+    static_assert(sizeof(float) == 4);
+
+    union { float f; uint8_t u[4]; } t{};
+    t.f = f;
+
+    return {
+        t.u[0], t.u[1], t.u[2], t.u[3]
+    };
+}
+
+float to_float(uint8_t u) {
+    return (float)u / (float)255;
+}
+
+uint8_t hash(uint8_t x) {
+    auto o = ((x ^ 0xAA) * 5);
+    auto rot = o % 8;
+    return o << rot | o >> (8 - rot);
+}
+
+float random() {
+    uint8_t r = std::rand() % 255;
+    return to_float(hash(r));
+}
+
+}
\ No newline at end of file