blob: 86305b4a0136c6067d838d189fe9b47088701917 (
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
42
|
#include <atomic>
#include <time.h>
#include <sys/time.h>
#include <random.hpp>
#include <math.hpp>
namespace rack {
namespace random {
thread_local Xoroshiro128Plus rng;
static std::atomic<uint64_t> threadCounter {0};
void init() {
// Don't reset state if already seeded
if (rng.isSeeded())
return;
// Get epoch time in microseconds for seed
struct timeval tv;
gettimeofday(&tv, NULL);
uint64_t usec = uint64_t(tv.tv_sec) * 1000 * 1000 + tv.tv_usec;
// Add number of initialized threads so far to random seed, so two threads don't get the same seed if initialized at the same time.
rng.seed(usec, threadCounter++);
// Shift state a few times due to low seed entropy
for (int i = 0; i < 4; i++) {
rng();
}
}
Xoroshiro128Plus& local() {
return rng;
}
} // namespace random
} // namespace rack
|