aboutsummaryrefslogtreecommitdiff
path: root/backend/RNG.cpp
blob: 21d50909be4d33033de286299a618ed133a3214b (plain)
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
#include "RNG.h"

using std::uniform_int_distribution;
using std::uniform_real_distribution;

RNG::RNG() : dev(), rng(dev()) { }

RNG & RNG::get() {
	static RNG instance;
	return instance;
}

std::mt19937 & RNG::get_engine() {
	return RNG::get().rng;
}

int RNG::rand_int(const int upper) {
	return this->rand_int(0, upper);
}
int RNG::rand_int(const int lower, const int upper) {
	// NOTE: random_dist's upper limit is inclusive
	uniform_int_distribution<int> random_dist(lower, upper - 1);
	return random_dist(rng);
}
int RNG::rand_int(const Range<int> range) {
	uniform_int_distribution<int> random_dist(range.min, range.max);
	return random_dist(rng);
}

double RNG::rand_double() {
	return this->rand_double(0.f, 1.f);
}
double RNG::rand_double(const double lower, const double upper) {
	uniform_real_distribution<double> random_dist(lower, upper);
	return random_dist(rng);
}

bool RNG::rand_bool() {
	return rand_int(0, 1);
}