1#include "duckdb/common/random_engine.hpp"
2#include "pcg_random.hpp"
3#include <random>
4
5namespace duckdb {
6
7struct RandomState {
8 RandomState() {
9 }
10
11 pcg32 pcg;
12};
13
14RandomEngine::RandomEngine(int64_t seed) : random_state(make_uniq<RandomState>()) {
15 if (seed < 0) {
16 random_state->pcg.seed(args: pcg_extras::seed_seq_from<std::random_device>());
17 } else {
18 random_state->pcg.seed(args&: seed);
19 }
20}
21
22RandomEngine::~RandomEngine() {
23}
24
25double RandomEngine::NextRandom(double min, double max) {
26 D_ASSERT(max >= min);
27 return min + (NextRandom() * (max - min));
28}
29
30double RandomEngine::NextRandom() {
31 return std::ldexp(x: random_state->pcg(), exp: -32);
32}
33uint32_t RandomEngine::NextRandomInteger() {
34 return random_state->pcg();
35}
36
37void RandomEngine::SetSeed(uint32_t seed) {
38 random_state->pcg.seed(args&: seed);
39}
40
41} // namespace duckdb
42