CDT++ 1.0.0-rc1
Causal Dynamical Triangulations in C++
Loading...
Searching...
No Matches
Random.hpp
Go to the documentation of this file.
1/*******************************************************************************
2 Causal Dynamical Triangulations in C++ using CGAL
3
4 Copyright © 2026 Adam Getchell
5 ******************************************************************************/
6
9
10#ifndef CDT_PLUSPLUS_RANDOM_HPP
11#define CDT_PLUSPLUS_RANDOM_HPP
12
13#include <concepts>
14#include <cstdint>
15#include <limits>
16#include <random>
17
18#include "pcg_random.hpp"
19
20namespace cdt
21{
22 using Random_seed = std::uint64_t;
23 using Random_stream = std::uint64_t;
24
25 namespace random_streams
26 {
27 inline Random_stream constexpr initialization{0};
28 inline Random_stream constexpr transitions{1};
29 } // namespace random_streams
30
42 class Random final
43 {
44 public:
45 using result_type = pcg64::result_type;
46
47 private:
48 Random_seed m_seed{};
49 Random_stream m_stream{};
50 pcg64 m_engine;
51
52 [[nodiscard]] static auto entropy_seed() -> Random_seed
53 {
54 std::random_device entropy;
55 std::uniform_int_distribution<Random_seed> distribution{
56 std::numeric_limits<Random_seed>::min(),
57 std::numeric_limits<Random_seed>::max()};
58 return distribution(entropy);
59 }
60
61 public:
63 Random() : Random{entropy_seed()} {}
64
69 explicit Random(Random_seed const seed, Random_stream const stream = 0)
70 : m_seed{seed}, m_stream{stream}, m_engine{seed, stream}
71 {}
72
73 [[nodiscard]] static auto constexpr min() noexcept -> result_type
74 { return pcg64::min(); }
75
76 [[nodiscard]] static auto constexpr max() noexcept -> result_type
77 { return pcg64::max(); }
78
79 [[nodiscard]] auto operator()() -> result_type { return m_engine(); }
80
82 [[nodiscard]] auto seed() const noexcept -> Random_seed { return m_seed; }
83
85 [[nodiscard]] auto stream() const noexcept -> Random_stream
86 { return m_stream; }
87
89 [[nodiscard]] auto split(Random_stream const stream) const -> Random
90 { return Random{m_seed, stream}; }
91 };
92
93 static_assert(std::uniform_random_bit_generator<Random>);
94} // namespace cdt
95
96#endif // CDT_PLUSPLUS_RANDOM_HPP
auto stream() const noexcept -> Random_stream
Definition Random.hpp:85
Random()
Construct a root stream from operating-system entropy.
Definition Random.hpp:63
auto split(Random_stream const stream) const -> Random
Create a fresh reproducible stream from the same root seed.
Definition Random.hpp:89
auto seed() const noexcept -> Random_seed
Definition Random.hpp:82
Random(Random_seed const seed, Random_stream const stream=0)
Construct a reproducible PCG stream without consulting entropy.
Definition Random.hpp:69