X-Git-Url: http://git.localhorst.tv/?a=blobdiff_plain;f=src%2Frand%2FGaloisLFSR.hpp;h=f5de7ad0bb650f15d9d6870d82e3bd6f3ee97b70;hb=846958003cd3529e6322146d289265859db0ba8e;hp=395ff8f0546cc7b75cf111a4b996a7705052f29b;hpb=41652fb3d73f12e6ae4ce7380244a75a4f5c6797;p=blank.git diff --git a/src/rand/GaloisLFSR.hpp b/src/rand/GaloisLFSR.hpp index 395ff8f..f5de7ad 100644 --- a/src/rand/GaloisLFSR.hpp +++ b/src/rand/GaloisLFSR.hpp @@ -12,7 +12,11 @@ class GaloisLFSR { public: // seed should be non-zero explicit GaloisLFSR(std::uint64_t seed) noexcept - : state(seed) { } + : state(seed) { + if (state == 0) { + state = 1; + } + } // get the next bit bool operator ()() noexcept { @@ -44,6 +48,23 @@ public: return (*this)(next); } + float SNorm() noexcept { + return float(Next()) * (1.0f / 2147483647.5f) - 1.0f; + } + + float UNorm() noexcept { + return float(Next()) * (1.0f / 4294967295.0f); + } + + template + typename Container::reference From(Container &c) { + return c[Next() % c.size()]; + } + template + typename Container::const_reference From(const Container &c) { + return c[Next() % c.size()]; + } + private: std::uint64_t state; // bits 64, 63, 61, and 60 set to 1 (counting from 1 lo to hi)