-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathtime_utils.cpp
More file actions
47 lines (38 loc) · 1.54 KB
/
time_utils.cpp
File metadata and controls
47 lines (38 loc) · 1.54 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 "time_utils.h"
int CLOCK_ID = CLOCK_THREAD_CPUTIME_ID;
void Time::printTimespec(timespec *ts) {
cout << ts->tv_sec << '.' << std::setfill('0') << std::setw(9) << ts->tv_nsec << " sec" << endl;
}
timespec* Time::getTimespecDelta(timespec *older,timespec *newer) {
newer->tv_nsec -= older->tv_nsec;
newer->tv_sec -= older->tv_sec;
if (newer->tv_nsec < 0) {
newer->tv_sec--;
newer->tv_nsec += 1000000000;
}
return newer;
}
double Time::getRandom() {
static random_device seed;
static UniformDistributionGenerator* generator =
new UniformDistributionGenerator(Engine(seed()),UniformDistribution(0,1));
return (*generator)();
}
double Time::getRandom(double left,double right) {
return getRandom()*(right - left) + left;
}
UniformDistributionGenerator Time::getUniformDistributionGenerator(double min,double max) {
static random_device seed;
return UniformDistributionGenerator(Engine(seed()),UniformDistribution(min,max));
}
GaussianDistributionGenerator Time::getGaussianDistributionGenerator(double M,double D) {
static random_device seed;
return GaussianDistributionGenerator(Engine(seed()),GaussianDistribution(M,D));
}
MaxwellDistributionSpeedGenerator Time::getMaxwellDistributionSpeedGenerator(double M,double D) {
return [M,D]() -> velocity {
//TODO check for M, D
static GaussianDistributionGenerator gdg = getGaussianDistributionGenerator(M/sqrt(3.0),D/sqrt(3.0));
return sqrt(pow(gdg(),2) + pow(gdg(),2) + pow(gdg(),2));
};
}