Last active
January 4, 2026 03:46
-
-
Save elbeno/a11852efffe55a5715293a6749a3f342 to your computer and use it in GitHub Desktop.
RNG recipe
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #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