-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrng.cpp
More file actions
46 lines (40 loc) · 1.19 KB
/
rng.cpp
File metadata and controls
46 lines (40 loc) · 1.19 KB
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
#include <cassert>
#include "rng.h"
namespace utils
{
namespace rand
{
// The definition of variable rng, declared in rng.h
Rng rng(static_cast<unsigned>(std::random_device()()));
// The definitions of the functions declared in rng.h
void seedRand(uint_fast32_t seed)
{
rng.seed(seed);
}
int randInt(int rangeBegin, int rangeEnd)
{
// Function to produce a random integer in [rangeBegin, rangeEnd).
assert(rangeEnd > rangeBegin);
std::uniform_int_distribution<> dist {rangeBegin, rangeEnd - 1};
return dist(rng);
}
double randDouble(double rangeBegin, double rangeEnd)
{
// Function to produce a random double in [rangeBegin, rangeEnd).
std::uniform_real_distribution<> dist {rangeBegin, rangeEnd};
return dist(rng);
}
double randNormal(double mean, double stdev)
{
// Function to produce a random normally distributed double.
std::normal_distribution<> dist {mean, stdev};
return dist(rng);
}
bool randBool()
{
// Function to produce a random bool
static std::bernoulli_distribution dist; // Default probability is 0.5.
return dist(rng);
}
}
}