1 #ifndef BLANK_RAND_GALOISLFSR_HPP_
2 #define BLANK_RAND_GALOISLFSR_HPP_
14 // seed should be non-zero
15 explicit GaloisLFSR(std::uint64_t seed) noexcept
23 bool operator ()() noexcept {
24 bool result = state & 1;
27 state |= 0x8000000000000000;
30 state &= 0x7FFFFFFFFFFFFFFF;
36 T operator ()(T &out) noexcept {
37 constexpr int num_bits =
38 std::numeric_limits<T>::digits +
39 std::numeric_limits<T>::is_signed;
40 for (int i = 0; i < num_bits; ++i) {
43 return out = static_cast<T>(state);
46 /// special case for randrom(boolean), since static_cast<bool>(0b10) == true
47 bool operator ()(bool &out) noexcept {
48 return out = operator ()();
57 float SNorm() noexcept {
58 return float(Next<std::uint32_t>()) * (1.0f / 2147483647.5f) - 1.0f;
61 float UNorm() noexcept {
62 return float(Next<std::uint32_t>()) * (1.0f / 4294967295.0f);
65 template<class Container>
66 typename Container::reference From(Container &c) {
68 return c[Next<typename Container::size_type>() % c.size()];
70 template<class Container>
71 typename Container::const_reference From(const Container &c) {
73 return c[Next<typename Container::size_type>() % c.size()];
78 // bits 64, 63, 61, and 60 set to 1 (counting from 1 lo to hi)
79 static constexpr std::uint64_t mask = 0xD800000000000000;