-
Notifications
You must be signed in to change notification settings - Fork 0
/
random.h
51 lines (44 loc) · 912 Bytes
/
random.h
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
51
#pragma once
#include <cmath>
class Random
{
public:
[[nodiscard]] unsigned long long operator()() noexcept
{
x = (a * x + c) % m;
return x;
}
void setseed(const unsigned long long seed) noexcept
{
x = seed;
}
private:
const unsigned long long c{ 1013904223 };
const unsigned long long a{ 1664525 };
const unsigned long long m{ static_cast<unsigned long long>(std::pow(2, 32)) };
unsigned long long x{ 1 };
};
class Hash
{
public:
[[nodiscard]] unsigned int operator()(unsigned int key) const noexcept
{
key += ~(key << 15);
key ^= key >> 10;
key += key << 3;
key ^= key >> 6;
key += ~(key << 11);
key ^= key >> 16;
return key;
}
[[nodiscard]] unsigned int operator()(const unsigned int x, const unsigned int y) const noexcept
{
return (*this)(x ^ (*this)(y ^ s));
}
void setseed(const unsigned int seed) noexcept
{
s = seed;
}
private:
unsigned int s{ 1 };
};