summary refs log tree commit diff
path: root/src/Math/Random.hpp
blob: d3636949e2bf40c419f0b290ba53436494e44aff (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
#pragma once

#include <array>
#include "Common.hpp"

namespace Math::Random {

std::array<U8, 4> break_float(F32 f);
F32 to_float(U8 u);

U8 hash(U8 x);

Real random();

template <USize D>
struct Noise {
    Real at(Vector<D> pos) const {
        U8 to_hash[D * 4];
        for (Int i = 0; i < D; i++) {
            auto b = break_float(pos[i]);
            to_hash[i*4] = b[0]; to_hash[i*4+1] = b[1]; to_hash[i*4+2] = b[2]; to_hash[i*4+3] = b[3];
        }
        U8 h = 0;
        for (Int i = 0; i < D * 4; i++) {
            h = hash(h) + to_hash[i];
        }
        return to_float(h);
    }
};

}