-
Notifications
You must be signed in to change notification settings - Fork 23
/
Copy pathRNG.cpp
65 lines (51 loc) · 948 Bytes
/
RNG.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
#include "RNG.h"
#include "Distributions/Cauchy.h"
#include "Utils.h"
#include <cmath>
namespace DNest4
{
bool RNG::randh_is_randh2 = false;
RNG::RNG()
:uniform(0., 1.)
,normal(0., 1.)
{
}
RNG::RNG(unsigned int seed)
:uniform(0., 1.)
,normal(0., 1.)
{
set_seed(seed);
}
void RNG::set_seed(unsigned int seed)
{
twister.seed(seed);
}
double RNG::rand()
{
return uniform(twister);
}
double RNG::randn()
{
return normal(twister);
}
double RNG::randt2()
{
// t-distribution with 2 degrees of freedom (less ridiculous than Cauchy)
return this->randn()/sqrt(-log(this->rand()));
}
double RNG::randh()
{
if(randh_is_randh2)
return randh2();
return pow(10.0, 1.5 - 3*std::abs(this->randt2()))*this->randn();
}
double RNG::randh2()
{
Cauchy cauchy;
return pow(10.0, 0.5 - std::abs(cauchy.generate(*this)))*this->randn();
}
int RNG::rand_int(int N)
{
return static_cast<int>(floor(N*this->rand()));
}
} // namespace DNest4