-
Notifications
You must be signed in to change notification settings - Fork 1
/
rng.h
57 lines (48 loc) · 1.96 KB
/
rng.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
52
53
54
55
56
57
/*
This file stems from a Fast Downward version dating around December 2014.
*/
#ifndef RNG_H
#define RNG_H
class RandomNumberGenerator {
static const int N = 624;
unsigned int mt[N];
int mti;
public:
RandomNumberGenerator(); // seed with time-dependent value
RandomNumberGenerator(int seed); // seed with int; see comments for seed()
RandomNumberGenerator(unsigned int *array, int count); // seed with array
RandomNumberGenerator(const RandomNumberGenerator ©);
RandomNumberGenerator &operator=(const RandomNumberGenerator ©);
void seed(int s);
void seed(unsigned int *array, int len);
unsigned int next32(); // random integer in [0..2^32-1]
int next31(); // random integer in [0..2^31-1]
double next_half_open(); // random float in [0..1), 2^53 possible values
double next_closed(); // random float in [0..1], 2^53 possible values
double next_open(); // random float in (0..1), 2^53 possible values
int next(int bound); // random integer in [0..bound), bound < 2^31
int operator()(int bound) { // same as next()
return next(bound);
}
double next(); // same as next_half_open()
double operator()() { // same as next_half_open()
return next_half_open();
}
};
/*
TODO: Add a static assertion that guarantees that ints are 32 bit.
In cases where they are not, need to adapt the code.
*/
/*
Notes on seeding
1. Seeding with an integer
To avoid different seeds mapping to the same sequence, follow one of
the following two conventions:
a) Only use seeds in 0..2^31-1 (preferred)
b) Only use seeds in -2^30..2^30-1 (2-complement machines only)
2. Seeding with an array (die-hard seed method)
The length of the array, len, can be arbitrarily high, but for lengths greater
than N, collisions are common. If the seed is of high quality, using more than
N values does not make sense.
*/
#endif