|
| 1 | +#ifndef RANDOM_UTILS_H |
| 2 | +#define RANDOM_UTILS_H |
| 3 | + |
| 4 | +#include <random> |
| 5 | +#include <vector> |
| 6 | +#include <algorithm> |
| 7 | +#include <type_traits> |
| 8 | +#include <chrono> |
| 9 | +#include <Rcpp.h> |
| 10 | + |
| 11 | +namespace utils |
| 12 | +{ |
| 13 | + |
| 14 | + // Abstract base class for random number generators |
| 15 | + class RNGBackend |
| 16 | + { |
| 17 | + public: |
| 18 | + virtual ~RNGBackend() = default; |
| 19 | + virtual void begin_rng() {} // Called before a sequence of random numbers |
| 20 | + virtual void end_rng() {} // Called after a sequence of random numbers |
| 21 | + virtual int random_int(int n) = 0; |
| 22 | + virtual double random_double() = 0; |
| 23 | + }; |
| 24 | + |
| 25 | + // Standard C++ random number generator backend |
| 26 | + class StdRNGBackend : public RNGBackend |
| 27 | + { |
| 28 | + private: |
| 29 | + static thread_local std::mt19937 generator; |
| 30 | + static bool seeded; |
| 31 | + |
| 32 | + public: |
| 33 | + void seed(uint32_t seed) |
| 34 | + { |
| 35 | + generator.seed(seed); |
| 36 | + seeded = true; |
| 37 | + } |
| 38 | + |
| 39 | + void initialize() |
| 40 | + { |
| 41 | + if (!seeded) |
| 42 | + { |
| 43 | + auto now = std::chrono::high_resolution_clock::now(); |
| 44 | + auto nanos = std::chrono::duration_cast<std::chrono::nanoseconds>( |
| 45 | + now.time_since_epoch()) |
| 46 | + .count(); |
| 47 | + generator.seed(static_cast<uint32_t>(nanos)); |
| 48 | + seeded = true; |
| 49 | + } |
| 50 | + } |
| 51 | + |
| 52 | + int random_int(int n) override |
| 53 | + { |
| 54 | + initialize(); |
| 55 | + std::uniform_int_distribution<int> dist(0, n - 1); |
| 56 | + return dist(generator); |
| 57 | + } |
| 58 | + |
| 59 | + double random_double() override |
| 60 | + { |
| 61 | + initialize(); |
| 62 | + std::uniform_real_distribution<double> dist(0.0, 1.0); |
| 63 | + return dist(generator); |
| 64 | + } |
| 65 | + }; |
| 66 | + |
| 67 | + // R random number generator backend |
| 68 | + class RcppRNGBackend : public RNGBackend |
| 69 | + { |
| 70 | + private: |
| 71 | + std::unique_ptr<Rcpp::RNGScope> rng_scope; |
| 72 | + |
| 73 | + public: |
| 74 | + void begin_rng() override |
| 75 | + { |
| 76 | + if (!rng_scope) |
| 77 | + { |
| 78 | + rng_scope = std::make_unique<Rcpp::RNGScope>(); |
| 79 | + } |
| 80 | + } |
| 81 | + |
| 82 | + void end_rng() override |
| 83 | + { |
| 84 | + rng_scope.reset(); |
| 85 | + } |
| 86 | + |
| 87 | + int random_int(int n) override |
| 88 | + { |
| 89 | + return static_cast<int>(unif_rand() * n); |
| 90 | + } |
| 91 | + |
| 92 | + double random_double() override |
| 93 | + { |
| 94 | + return unif_rand(); |
| 95 | + } |
| 96 | + }; |
| 97 | + |
| 98 | + class RandomGenerator |
| 99 | + { |
| 100 | + private: |
| 101 | + static RNGBackend *backend; |
| 102 | + static StdRNGBackend std_backend; |
| 103 | + static RcppRNGBackend rcpp_backend; |
| 104 | + |
| 105 | + public: |
| 106 | + // RAII class to handle RNG state |
| 107 | + class RNGScope |
| 108 | + { |
| 109 | + private: |
| 110 | + RNGBackend *backend; |
| 111 | + |
| 112 | + public: |
| 113 | + RNGScope() : backend(RandomGenerator::backend) |
| 114 | + { |
| 115 | + backend->begin_rng(); |
| 116 | + } |
| 117 | + ~RNGScope() |
| 118 | + { |
| 119 | + backend->end_rng(); |
| 120 | + } |
| 121 | + }; |
| 122 | + |
| 123 | + // Switch to using R's RNG |
| 124 | + static void use_r_random() |
| 125 | + { |
| 126 | + backend = &rcpp_backend; |
| 127 | + } |
| 128 | + |
| 129 | + // Switch to using C++ standard RNG |
| 130 | + static void use_std_random() |
| 131 | + { |
| 132 | + backend = &std_backend; |
| 133 | + } |
| 134 | + |
| 135 | + // Initialize the standard generator with a seed |
| 136 | + static void seed(uint32_t seed) |
| 137 | + { |
| 138 | + std_backend.seed(seed); |
| 139 | + } |
| 140 | + |
| 141 | + // Generate random integer in range [0, n) |
| 142 | + static int random_index(int n) |
| 143 | + { |
| 144 | + RNGScope scope; |
| 145 | + return backend->random_int(n); |
| 146 | + } |
| 147 | + |
| 148 | + // Generate random double in range [0, 1) |
| 149 | + static double random_double() |
| 150 | + { |
| 151 | + RNGScope scope; |
| 152 | + return backend->random_double(); |
| 153 | + } |
| 154 | + |
| 155 | + // Shuffle a range of elements |
| 156 | + template <typename Iter> |
| 157 | + static void shuffle(Iter first, Iter last) |
| 158 | + { |
| 159 | + RNGScope scope; |
| 160 | + auto n = std::distance(first, last); |
| 161 | + for (auto i = n - 1; i > 0; --i) |
| 162 | + { |
| 163 | + std::swap(*(first + i), *(first + backend->random_int(i + 1))); |
| 164 | + } |
| 165 | + } |
| 166 | + |
| 167 | + // Sample n elements with replacement |
| 168 | + template <typename T> |
| 169 | + static std::vector<T> sample_with_replacement(const std::vector<T> &population, size_t n) |
| 170 | + { |
| 171 | + RNGScope scope; |
| 172 | + std::vector<T> result; |
| 173 | + result.reserve(n); |
| 174 | + for (size_t i = 0; i < n; ++i) |
| 175 | + { |
| 176 | + result.push_back(population[backend->random_int(population.size())]); |
| 177 | + } |
| 178 | + return result; |
| 179 | + } |
| 180 | + }; |
| 181 | + |
| 182 | +} // namespace utils |
| 183 | + |
| 184 | +#endif // RANDOM_UTILS_H |
0 commit comments