-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathrandom.h
More file actions
37 lines (29 loc) · 693 Bytes
/
random.h
File metadata and controls
37 lines (29 loc) · 693 Bytes
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
#ifndef _RANDOM_H_
#define _RANDOM_H_
#include <climits>
namespace edubpt {
// Xor-Shiftによる乱数ジェネレータ
class XorShift {
unsigned int seed_[4];
public:
unsigned int next(void) {
const unsigned int t = seed_[0] ^ (seed_[0] << 11);
seed_[0] = seed_[1];
seed_[1] = seed_[2];
seed_[2] = seed_[3];
return seed_[3] = (seed_[3] ^ (seed_[3] >> 19)) ^ (t ^ (t >> 8));
}
// [0, 1)
double next01(void) {
return (double)next() / (UINT_MAX + 1.0);
}
XorShift(const unsigned int initial_seed) {
unsigned int s = initial_seed;
for (int i = 1; i <= 4; i++){
seed_[i-1] = s = 1812433253U * (s^(s>>30)) + i;
}
}
};
typedef XorShift Random;
};
#endif