Monte Carlo Integration Library 1.0
High-performance Monte Carlo methods for numerical integration and optimization
rng_global.cpp
Go to the documentation of this file.
1#include "rng_global.hpp"
2#include <atomic>
3
4namespace mc {
5namespace rng {
6namespace detail{
7
8 // Default seed (used if never explicitly initialized)
9 std::atomic<std::uint32_t> g_global_seed{12345u};
10
11 // Tracks whether the seed has been explicitly set
12 std::atomic<bool> g_seed_initialized{false};
13
14} //namespace detail
15
16bool set_global_seed(std::uint32_t s) {
17 bool expected = false;
18
19 // Allow setting the seed ONLY if it was never initialized
20 if (mc::rng::detail::g_seed_initialized.compare_exchange_strong(
21 expected,
22 true,
23 std::memory_order_acq_rel))
24 {
25 mc::rng::detail::g_global_seed.store(s, std::memory_order_relaxed);
26 return true;
27 }
28
29 return false;
30}
31
32std::uint32_t get_global_seed() {
33 return mc::rng::detail::g_global_seed.load(std::memory_order_relaxed);
34}
35
37 return mc::rng::detail::g_seed_initialized.load(std::memory_order_acquire);
38}
39
40} // namespace rng
41} // namespace mc
std::atomic< bool > g_seed_initialized
std::atomic< std::uint32_t > g_global_seed
Definition rng_global.cpp:9
bool is_global_seed_initialized()
Check whether the global seed has been explicitly initialized.
bool set_global_seed(std::uint32_t s)
Set the global seed used by all library RNG components.
std::uint32_t get_global_seed()
Get the current global seed.