-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathRandom.h
58 lines (48 loc) · 1.56 KB
/
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
52
53
54
55
56
57
58
#ifndef RANDOM_H
#define RANDOM_H
/*
Random number generator class
=============================
History:
Created - Sarah "Voodoo Doll" White (2006/01/24)
=============================
Description:
This class wraps the Mersenne Twister generator
with a public interface that supports three common
pseudorandom number requests:
=== Uniform deviate [0,1) ===
Random rnd(seed);
double r = rnd.uniform();
=== Uniform deviate [0,hi) ===
Random rnd(seed);
unsigned long r = rnd.uniform(hi);
=== Uniform deviate [lo,hi) ===
Random rnd(seed);
unsigned long r = rnd.uniform(lo, hi);
seed, lo, and hi are user supplied values, with
seed having a default setting of 1 for debugging
and testing purposes.
*/
class Random {
// Arbitrary constants that work well
static const int N = 624;
static const int M = 397;
static const unsigned long MATRIX_A = 0x9908b0dfUL;
static const unsigned long UPPER_MASK = 0x80000000UL;
static const unsigned long LOWER_MASK = 0x7fffffffUL;
static const unsigned long MAX = 0xffffffffUL;
unsigned long x[N]; // Random number pool
int next; // Current pool index
public:
Random(unsigned long seed = 1) : next(0) { seedgen(seed); }
// Return a uniform deviate in the range [0,1)
double uniform();
// Return a uniform deviate in the range [0,hi)
unsigned uniform(unsigned hi);
// Return a uniform deviate in the range [lo,hi)
unsigned uniform(unsigned lo, unsigned hi);
private:
void seedgen(unsigned long seed);
unsigned long randgen();
};
#endif // RANDOM_H