-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathmain.cpp
68 lines (58 loc) · 1.5 KB
/
main.cpp
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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
#include "pch.h"
class RandomANSIC {
public:
RandomANSIC(int seed) { srand(seed); }
int next() { return rand(); }
int max() const { return RAND_MAX; }
};
class RandomVC {
public:
RandomVC(int seed): mSeed(seed << 15) { }
int next() {
mSeed *= 214013;
mSeed += 2531011;
return (mSeed >> 15) & 0x7fff;
}
int max() const { return 1 << 15; }
private:
unsigned mSeed;
};
class RandomGCC {
public:
RandomGCC(int seed): mSeed(seed << 15) { }
int next() {
mSeed *= 1103515245;
mSeed += 12345;
return (mSeed >> 15) & 0x7fff;
}
int max() const { return 1 << 15; }
private:
unsigned mSeed;
};
template<typename RandomT>
static void testRandom(int seed) {
RandomT random(seed);
int lens[] = {256, 4 * 1024, 16 * 1024};
vector<int> v;
for (int len : lens) {
v.clear();
v.resize(len, 0);
const int AVG = 4 * 1024;
const double MISS_RATE = 0.1;
for (int i = 0; i < AVG * (int)v.size(); ++i) {
unsigned r = random.next();
if(random.max() < v.size()) r = r * random.max() + random.next();
++v[r % v.size()];
}
for (int i = 0; i < (int)v.size(); ++i) {
assert(fabs(v[i] / double(AVG) - 1) < MISS_RATE);
}
}
}
#define TEST(type, seed) puts("test: " #type); testRandom<type>(seed);
int main() {
int seed = (int)time(nullptr);
TEST(RandomANSIC, seed);
TEST(RandomVC, seed);
TEST(RandomGCC, seed);
}