1 #ifndef BLOBS_RAND_GALOISLFSR_HPP_
2 #define BLOBS_RAND_GALOISLFSR_HPP_
15 // seed should be non-zero
16 explicit GaloisLFSR(std::uint64_t seed) noexcept
24 bool operator ()() noexcept {
25 bool result = state & 1;
28 state |= 0x8000000000000000;
31 state &= 0x7FFFFFFFFFFFFFFF;
37 T operator ()(T &out) noexcept {
38 constexpr int num_bits =
39 std::numeric_limits<T>::digits +
40 std::numeric_limits<T>::is_signed;
41 for (int i = 0; i < num_bits; ++i) {
44 return out = static_cast<T>(state);
47 /// special case for randrom(boolean), since static_cast<bool>(0b10) == true
48 bool operator ()(bool &out) noexcept {
49 return out = operator ()();
58 float SNorm() noexcept {
59 return float(Next<std::uint32_t>()) * (1.0f / 2147483647.5f) - 1.0f;
62 float UNorm() noexcept {
63 return float(Next<std::uint32_t>()) * (1.0f / 4294967295.0f);
66 template<class Container>
67 typename Container::reference From(Container &c) {
69 return c[Next<typename Container::size_type>() % c.size()];
71 template<class Container>
72 typename Container::const_reference From(const Container &c) {
74 return c[Next<typename Container::size_type>() % c.size()];
79 // bits 64, 63, 61, and 60 set to 1 (counting from 1 lo to hi)
80 static constexpr std::uint64_t mask = 0xD800000000000000;