-
Notifications
You must be signed in to change notification settings - Fork 2
/
Rng.cpp
78 lines (73 loc) · 2.11 KB
/
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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
#define RNG_CPP
#include "Rng.h"
#include <iostream>
#include <algorithm>
#include <chrono>
#include <mach/mach_time.h>
#define ORWL_NANO (+1.0E-9)
#define ORWL_GIGA UINT64_C(1000000000)
static double orwl_timebase = 0.0;
static uint64_t orwl_timestart = 0;
struct timespec orwl_gettime(void) {
// be more careful in a multithreaded environement
if (!orwl_timestart) {
mach_timebase_info_data_t tb = { 0 };
mach_timebase_info(&tb);
orwl_timebase = tb.numer;
orwl_timebase /= tb.denom;
orwl_timestart = mach_absolute_time();
}
struct timespec t;
double diff = (mach_absolute_time() - orwl_timestart) * orwl_timebase;
t.tv_sec = diff * ORWL_NANO;
t.tv_nsec = diff - (t.tv_sec * ORWL_GIGA);
return t;
}
RNG::RNG() : stdNormal(0, 1), uniform01(0, 1) {
// auto seed =
// std::chrono::high_resolution_clock::now().time_since_epoch().count();
// std::mt19937 gen(seed);
timespec ts=orwl_gettime();
// clock_gettime(CLOCK_REALTIME, &ts);
RNGseedGeneratorMutex.lock();
gen.seed(RNGseedGenerator() + ts.tv_nsec);
RNGseedGeneratorMutex.unlock();
}
int RNG::randint(int n) {
if (n == 0)
return 0;
else
return gen() % n;
}
float RNG::uniform(float a, float b) { return a + (b - a) * uniform01(gen); }
float RNG::normal(float mean, float sd) { return mean + sd * stdNormal(gen); }
int RNG::bernoulli(float p) {
if (uniform01(gen) < p)
return 1;
else
return 0;
}
template <typename T> int RNG::index(std::vector<T> &v) {
if (v.size() == 0)
std::cout << "RNG::index called for empty std::vector!\n";
return gen() % v.size();
}
std::vector<int> RNG::NchooseM(int n, int m) {
std::vector<int> ret(m, 100);
int ctr = m;
for (int i = 0; i < n; i++)
if (uniform01(gen) < ctr * 1.0 / (n - i))
ret[m - ctr--] = i;
return ret;
}
std::vector<int> RNG::permutation(int n) {
std::vector<int> ret;
for (int i = 0; i < n; i++)
ret.push_back(i);
std::shuffle(ret.begin(), ret.end(), gen);
return ret;
}
template <typename T> void RNG::vectorShuffle(std::vector<T> &v) {
std::shuffle(v.begin(), v.end(), gen);
}
template void RNG::vectorShuffle<int>(std::vector<int> &v);