summary refs log tree commit diff
path: root/src/Math/Random.cpp
blob: aa0ff55223c79e0589e45068832bb9a3318e5899 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
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));
}

}