summary refs log tree commit diff
path: root/src/Math/Random.hpp
blob: ab948719118a0e106845f60b18a72c4666d04253 (plain) pre { line-height: 125%; } td.linenos .normal { color: inherit; background-color: transparent; padding-left: 5px; padding-right: 5px; } span.linenos { color: inherit; background-color: transparent; padding-left: 5px; padding-right: 5px; } td.linenos .special { color: #000000; background-color: #ffffc0; padding-left: 5px; padding-right: 5px; } span.linenos.special { color: #000000; background-color: #ffffc0; padding-left: 5px; padding-right: 5px; } .highlight .hll { background-color: #0000ff } .highlight .c { color: #0F0 } /* Comment */ .highlight .err { color: #DDD } /* Error */ .highlight .esc { color: #DDD } /* Escape */ .highlight .g { color: #DDD } /* Generic */ .highlight .k { color: #F00 } /* Keyword */ .highlight .l { color: #DDD } /* Literal */ .highlight .n { color: #DDD } /* Name */ .highlight .o { color: #DDD } /* Operator */ .highlight .x { color: #DDD } /* Other */ .highlight .p { color: #DDD } /* Punctuation */ .highlight .ch { color: #0F0 } /* Comment.Hashbang */ .
#pragma once

#include "array"
#include "Common.hpp"

namespace Math::Random {

std::array<uint8_t, 4> break_float(float f);
float to_float(uint8_t u);

uint8_t hash(uint8_t x);

float random();

template <size_t D>
struct Noise {
    float at(Vector<D> pos) const {
        uint8_t 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];
        }
        uint8_t h = 0;
        for (int i = 0; i < D * 4; i++) {
            h = hash(h) + to_hash[i];
        }
        return to_float(h);
    }
};

}