Vulcan
Aerospace Engineering Utilities Built on Janus
Loading...
Searching...
No Matches
RNG.hpp
Go to the documentation of this file.
1
8
9#pragma once
10
11#include <Eigen/Core>
12#include <cstdint>
13#include <random>
14
15namespace vulcan::rng {
16
37class RNG {
38 public:
39 using Engine = std::mt19937_64;
40 using result_type = Engine::result_type;
41
46 explicit RNG(uint64_t seed)
47 : engine_(seed), seed_(seed), normal_(0.0, 1.0), uniform_(0.0, 1.0) {}
48
56 explicit RNG(std::seed_seq &seq)
57 : engine_(seq), seed_(0), normal_(0.0, 1.0), uniform_(0.0, 1.0) {}
58
59 // Prevent copying (each RNG should have unique state)
60 RNG(const RNG &) = delete;
61 RNG &operator=(const RNG &) = delete;
62
63 // Allow moving
64 RNG(RNG &&) = default;
65 RNG &operator=(RNG &&) = default;
66
71 result_type operator()() { return engine_(); }
72
77 double gaussian() { return normal_(engine_); }
78
85 double gaussian(double mean, double stddev) {
86 return mean + stddev * normal_(engine_);
87 }
88
93 double uniform() { return uniform_(engine_); }
94
101 double uniform(double min, double max) {
102 return min + (max - min) * uniform_(engine_);
103 }
104
111 int64_t uniform_int(int64_t min, int64_t max) {
112 std::uniform_int_distribution<int64_t> dist(min, max);
113 return dist(engine_);
114 }
115
120 Eigen::Vector3d gaussian3() {
121 return Eigen::Vector3d(normal_(engine_), normal_(engine_),
122 normal_(engine_));
123 }
124
130 template <int N> Eigen::Vector<double, N> gaussianN() {
131 Eigen::Vector<double, N> v;
132 for (int i = 0; i < N; ++i) {
133 v(i) = normal_(engine_);
134 }
135 return v;
136 }
137
142 Engine &engine() { return engine_; }
143 const Engine &engine() const { return engine_; }
144
150 uint64_t seed() const { return seed_; }
151
156 void discard(uint64_t n) { engine_.discard(n); }
157
163 void reset() {
164 engine_.seed(seed_);
165 normal_.reset(); // Clear Box-Muller cache
166 uniform_.reset(); // Clear any distribution cache
167 }
168
173 void reseed(uint64_t new_seed) {
174 seed_ = new_seed;
175 engine_.seed(new_seed);
176 normal_.reset();
177 uniform_.reset();
178 }
179
180 private:
181 Engine engine_;
182 uint64_t seed_;
183 std::normal_distribution<double> normal_;
184 std::uniform_real_distribution<double> uniform_;
185};
186
187} // namespace vulcan::rng
int64_t uniform_int(int64_t min, int64_t max)
Generate uniform integer [min, max].
Definition RNG.hpp:111
double uniform(double min, double max)
Generate uniform [min, max).
Definition RNG.hpp:101
Eigen::Vector< double, N > gaussianN()
Fill N-vector with independent N(0,1).
Definition RNG.hpp:130
RNG & operator=(RNG &&)=default
RNG(const RNG &)=delete
RNG(RNG &&)=default
double gaussian()
Generate standard normal N(0,1).
Definition RNG.hpp:77
result_type operator()()
Get raw random bits.
Definition RNG.hpp:71
double gaussian(double mean, double stddev)
Generate normal N(mean, stddev).
Definition RNG.hpp:85
RNG(std::seed_seq &seq)
Construct with seed sequence.
Definition RNG.hpp:56
Engine & engine()
Access underlying engine (for advanced use).
Definition RNG.hpp:142
void reseed(uint64_t new_seed)
Reseed with new value.
Definition RNG.hpp:173
uint64_t seed() const
Get original seed (for logging/reproducibility).
Definition RNG.hpp:150
Engine::result_type result_type
Definition RNG.hpp:40
const Engine & engine() const
Definition RNG.hpp:143
RNG & operator=(const RNG &)=delete
RNG(uint64_t seed)
Construct with explicit seed.
Definition RNG.hpp:46
void reset()
Reset to initial state with same seed.
Definition RNG.hpp:163
std::mt19937_64 Engine
Definition RNG.hpp:39
double uniform()
Generate uniform [0, 1).
Definition RNG.hpp:93
Eigen::Vector3d gaussian3()
Fill 3-vector with independent N(0,1).
Definition RNG.hpp:120
void discard(uint64_t n)
Discard n values (for stream splitting).
Definition RNG.hpp:156
Definition Distributions.hpp:14