aboutsummaryrefslogtreecommitdiffstats
path: root/util/random/mersenne64.h
blob: bb02b3c53bf20646fd2178dfbdd5099f79681461 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
#pragma once
 
#include <util/system/defaults.h> 
 
class IInputStream;
 
namespace NPrivate { 
    class TMersenne64 { 
        static constexpr int NN = 312;
 
    public: 
        inline TMersenne64(ui64 s = ULL(19650218)) 
            : mti(NN + 1) 
        { 
            InitGenRand(s); 
        } 
 
        inline TMersenne64(const ui64* keys, size_t len) noexcept
            : mti(NN + 1) 
        { 
            InitByArray(keys, len); 
        } 
 
        TMersenne64(IInputStream& input);
 
        inline ui64 GenRand() noexcept {
            if (mti >= NN) { 
                InitNext(); 
            } 
 
            ui64 x = mt[mti++]; 
 
            x ^= (x >> 29) & ULL(0x5555555555555555); 
            x ^= (x << 17) & ULL(0x71D67FFFEDA60000); 
            x ^= (x << 37) & ULL(0xFFF7EEE000000000); 
            x ^= (x >> 43); 
 
            return x; 
        } 
 
    private: 
        void InitNext() noexcept;
        void InitGenRand(ui64 seed) noexcept;
        void InitByArray(const ui64* init_key, size_t key_length) noexcept;
 
    private: 
        ui64 mt[NN]; 
        int mti; 
    }; 
}