-
Notifications
You must be signed in to change notification settings - Fork 4
/
utilities.h
65 lines (52 loc) · 1.34 KB
/
utilities.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
#ifndef UTILITIES_H_
#define UTILITIES_H_
#include <cstdlib>
#include <ctime>
#include <iostream>
#include <fstream>
#include <vector>
#include <cmath>
#ifndef WIN32
#endif
using namespace std;
//! Returns a random number in [0, 1]
inline double randDouble() {
return rand() / (RAND_MAX + 1.0);
}
//! Returns a random number in [min, max]
inline double randomFromRange(const double &minRange, const double &maxRange) {
return minRange + (maxRange - minRange) * randDouble();
}
//! Random permutations
void randPerm(const int &inNum, vector<int> &outVect);
void randPerm(const int &inNum, const int inPart, vector<int> &outVect);
inline void fillWithRandomNumbers(const int &length, vector<double> &inVect) {
inVect.clear();
for (int i = 0; i < length; i++) {
inVect.push_back(2.0 * (randDouble() - 0.5));
}
}
inline double sum(const vector<double> &inVect) {
double val = 0.0;
vector<double>::const_iterator itr(inVect.begin()), end(inVect.end());
while (itr != end) {
val += *itr;
++itr;
}
return val;
}
//! Poisson sampling
inline int poisson(double A) {
int k = 0;
int maxK = 10;
while (1) {
double U_k = randDouble();
A *= U_k;
if (k > maxK || A < exp(-1.0)) {
break;
}
k++;
}
return k;
}
#endif /* UTILITIES_H_ */