| 1 | //===----------------------------------------------------------------------===// |
| 2 | // DuckDB |
| 3 | // |
| 4 | // duckdb/common/random_engine.hpp |
| 5 | // |
| 6 | // |
| 7 | //===----------------------------------------------------------------------===// |
| 8 | |
| 9 | #pragma once |
| 10 | |
| 11 | #include "duckdb/common/common.hpp" |
| 12 | #include "duckdb/common/limits.hpp" |
| 13 | #include "duckdb/common/mutex.hpp" |
| 14 | |
| 15 | #include <random> |
| 16 | |
| 17 | namespace duckdb { |
| 18 | class ClientContext; |
| 19 | struct RandomState; |
| 20 | |
| 21 | struct RandomEngine { |
| 22 | RandomEngine(int64_t seed = -1); |
| 23 | ~RandomEngine(); |
| 24 | |
| 25 | public: |
| 26 | //! Generate a random number between min and max |
| 27 | double NextRandom(double min, double max); |
| 28 | |
| 29 | //! Generate a random number between 0 and 1 |
| 30 | double NextRandom(); |
| 31 | uint32_t NextRandomInteger(); |
| 32 | |
| 33 | void SetSeed(uint32_t seed); |
| 34 | |
| 35 | static RandomEngine &Get(ClientContext &context); |
| 36 | |
| 37 | mutex lock; |
| 38 | |
| 39 | private: |
| 40 | unique_ptr<RandomState> random_state; |
| 41 | }; |
| 42 | |
| 43 | } // namespace duckdb |
| 44 | |