stellar

Stellar - UCI Chess engine written in C++20
git clone git://git.dimitrijedobrota.com/stellar.git
Log | Files | Refs | README | LICENSE

random.hpp (854B)


      1 #ifndef STELLAR_RANDOM_H
      2 #define STELLAR_RANDOM_H
      3 
      4 #include "utils.hpp"
      5 
      6 class Random {
      7   public:
      8     constexpr Random(void) = default;
      9     constexpr Random(U64 seed) : state(seed) {}
     10 
     11     constexpr U64 operator()(void) { return get_U64(); }
     12 
     13     constexpr U32 get_U32(void) {
     14         U32 number = state;
     15 
     16         number ^= number << 13;
     17         number ^= number >> 17;
     18         number ^= number << 5;
     19 
     20         return state = number;
     21     }
     22 
     23     constexpr U64 get_U64(void) {
     24         U64 n1, n2, n3, n4;
     25 
     26         n1 = (U64)(get_U32()) & C64(0xFFFF);
     27         n2 = (U64)(get_U32()) & C64(0xFFFF);
     28         n3 = (U64)(get_U32()) & C64(0xFFFF);
     29         n4 = (U64)(get_U32()) & C64(0xFFFF);
     30 
     31         return n1 | (n2 << 16) | (n3 << 32) | (n4 << 48);
     32     }
     33 
     34   private:
     35     static const U32 default_seed = C32(1804289383);
     36     U32 state = default_seed;
     37 };
     38 
     39 #endif