Skip to content

Instantly share code, notes, and snippets.

@elbeno
Last active January 4, 2026 03:46
Show Gist options
  • Select an option

  • Save elbeno/a11852efffe55a5715293a6749a3f342 to your computer and use it in GitHub Desktop.

Select an option

Save elbeno/a11852efffe55a5715293a6749a3f342 to your computer and use it in GitHub Desktop.
RNG recipe
#include <algorithm>
#include <array>
#include <functional>
#include <iterator>
#include <random>
template <typename Generator = std::mt19937>
[[nodiscard]] auto get_rng() -> auto & {
thread_local auto rng = [] {
auto seed_data = std::array<int, Generator::state_size>{};
auto r = std::random_device{};
std::ranges::generate(seed_data, std::ref(r));
auto seq = std::seed_seq{std::begin(seed_data), std::end(seed_data)};
return Generator{seq};
}();
return rng;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment