X-Git-Url: http://git.localhorst.tv/?a=blobdiff_plain;f=src%2Frand%2FGaloisLFSR.hpp;h=f8a61e23f3f7c721689d7e0ff4a2404aa671f412;hb=58c8fefb8cf80322ae165830c117ac1e3e34d32b;hp=2ee476b6e321cc5d0298993b75e134b0727aeaad;hpb=825f479edf9867938b6789215ad7ae6303596cba;p=blank.git diff --git a/src/rand/GaloisLFSR.hpp b/src/rand/GaloisLFSR.hpp index 2ee476b..f8a61e2 100644 --- a/src/rand/GaloisLFSR.hpp +++ b/src/rand/GaloisLFSR.hpp @@ -1,6 +1,7 @@ #ifndef BLANK_RAND_GALOISLFSR_HPP_ #define BLANK_RAND_GALOISLFSR_HPP_ +#include #include #include @@ -42,18 +43,33 @@ public: return out = static_cast(state); } + /// special case for randrom(boolean), since static_cast(0b10) == true + bool operator ()(bool &out) noexcept { + return out = operator ()(); + } + template T Next() noexcept { T next; 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) { + assert(c.size() > 0); return c[Next() % c.size()]; } template typename Container::const_reference From(const Container &c) { + assert(c.size() > 0); return c[Next() % c.size()]; }