summary refs log tree commit diff
path: root/src/Math/Random.cpp
blob: cf3bbad8cab7fb283a20e825beb67f27b01d8bbf (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
#include "Random.hpp"

namespace Math::Random {

std::array<U8, 4> break_float(const F32 f) {
    union { F32 f; U8 u[4]; } t{};
    t.f = f;

    return {
        t.u[0], t.u[1], t.u[2], t.u[3]
    };
}

F32 to_float(U8 u) {
    return (F32)u / (F32)255;
}

U8 hash(U8 x) {
    auto o = (x ^ 0xAA) * 5;
    auto rot = o % 8;
    return o << rot | o >> (8 - rot);
}

Real random() {
    U8 r = std::rand() % 255;
    return to_float(hash(r));
}

}