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