-
Notifications
You must be signed in to change notification settings - Fork 7
/
MersenneTwister.h
140 lines (111 loc) · 4.11 KB
/
MersenneTwister.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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
/*
Copyright (c) 2015-2017 Lester Hedges <[email protected]>
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#ifndef _MERSENNETWISTER_H
#define _MERSENNETWISTER_H
#include <random>
/*! \file MersenneTwister.h
\brief A C++11 implementation of a Mersenne-Twister
random number generator class.
*/
namespace slsm
{
//! Mersenne-Twister class.
class MersenneTwister
{
public:
//! Constructor.
MersenneTwister()
{
// Get a hardware random number and seed the generator.
seed = std::random_device{}();
generator.seed(seed);
/* Note that seeding the generator with a single 32-bit integer
only allows for 2^32 initial states.
For a better RNG, consider using the Permuted Congruential Generator
http://www.pcg-random.org
For details on the pitfalls of seeding the C++11 mt19937 generator,
see
http://www.pcg-random.org/posts/cpp-seeding-surprises.html
To enable good seeding with mt19937, consider using randutils.hpp
https://gist.github.com/imneme/540829265469e673d045
*/
}
//! Overloaded () operator.
/*! \return A uniform random double in range [0-1]. */
double operator()()
{
return default_uniform_real_distribution(generator);
}
//! Generate a random integer between min and max (inclusive).
/*! \param min
The minium of the range.
\param max
The maxium of the range.
\return
The uniform random integer.
*/
int integer(int min, int max)
{
return std::uniform_int_distribution<int>{min, max}(generator);
}
//! Generate a random number from a normal distribution with
/*! zero mean and unit standard deviation.
\return
A random number drawn from the normal distribution.
*/
double normal()
{
return default_normal_distribution(generator);
}
//! Generate a random number from a normal distribution.
/*! \param mean
The mean of the the normal distribution.
\param stdDev
The standard deviation of the normal distribution.
\return
A random number drawn from the normal distribution.
*/
double normal(double mean, double stdDev)
{
return std::normal_distribution<double>{mean, stdDev}(generator);
}
//! Get the random number generator seed.
/*! \return seed
The generator seed.
*/
unsigned int getSeed()
{
return seed;
}
//! Seed the random number generator.
/*! \param seed_
The new seed.
*/
void setSeed(unsigned int seed_)
{
seed = seed_;
generator.seed(seed);
}
private:
/// The Mersenne-Twister generator.
std::mt19937 generator;
/// Default uniform_real distribution [0-1].
std::uniform_real_distribution<double> default_uniform_real_distribution{0.0, 1.0};
/// Default normal distribution with zero mean and unit standard deviation.
std::normal_distribution<double> default_normal_distribution{0.0, 1.0};
/// The random number seed.
unsigned int seed;
};
}
#endif /* _MERSENNETWISTER_H */