X-Git-Url: http://git.localhorst.tv/?a=blobdiff_plain;f=src%2Frand%2FGaloisLFSR.hpp;h=2ee476b6e321cc5d0298993b75e134b0727aeaad;hb=d5ef462f3bd70a9717c1309466ce8d90b038c5cd;hp=395ff8f0546cc7b75cf111a4b996a7705052f29b;hpb=41652fb3d73f12e6ae4ce7380244a75a4f5c6797;p=blank.git diff --git a/src/rand/GaloisLFSR.hpp b/src/rand/GaloisLFSR.hpp index 395ff8f..2ee476b 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,15 @@ public: return (*this)(next); } + 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)