aegir
Phlex-based simulation framework for the SHiP experiment.
Loading...
Searching...
No Matches
seed_config.hpp
Go to the documentation of this file.
1// SPDX-FileCopyrightText: 2026 CERN for the benefit of the SHiP Collaboration
2//
3// SPDX-License-Identifier: LGPL-3.0-or-later
4
5// seed_config.hpp — shared resolution of the optional `seed` config key
6//
7// Reproducibility is opt-in: without a configured seed, draw one at random
8// so independent jobs produce independent output, and log it so any run can
9// still be reproduced after the fact. Parsed as int64 so the full uint32
10// range survives — drawn seeds are logged as uint32 values, and pasting one
11// back must round-trip.
12
13#pragma once
14
15#include <spdlog/spdlog.h>
16
17#include <cstdint>
18#include <limits>
19#include <random>
20#include <stdexcept>
21#include <string>
22
23#include "phlex/configuration.hpp"
24
25namespace aegir {
26
27[[nodiscard]] inline std::uint32_t resolve_seed(
28 phlex::configuration const& config, std::string const& component) {
29 auto const configured_seed = config.get_if_present<std::int64_t>("seed");
30 if (configured_seed &&
31 (*configured_seed < 0 ||
32 *configured_seed > std::numeric_limits<std::uint32_t>::max()))
33 throw std::runtime_error(component + ": seed " +
34 std::to_string(*configured_seed) +
35 " is outside the valid range [0, 4294967295]");
36 auto const seed = configured_seed
37 ? static_cast<std::uint32_t>(*configured_seed)
38 : static_cast<std::uint32_t>(std::random_device{}());
39 if (!configured_seed)
40 spdlog::info(
41 "{}: no seed configured — drew random seed {}; set 'seed: {}' to "
42 "reproduce this run",
43 component, seed, seed);
44 return seed;
45}
46
47} // namespace aegir
Definition math_utils.hpp:12
std::uint32_t resolve_seed(phlex::configuration const &config, std::string const &component)
Definition seed_config.hpp:27