From c65b09b824bdc213615fb9eb0c848b5bfc56dd56 Mon Sep 17 00:00:00 2001 From: raagrawal <88426170+raagrawal@users.noreply.github.com> Date: Wed, 10 Jun 2026 14:46:36 +0530 Subject: [PATCH] Encapsulate global random seed in a function Refactor global random seed management to use a function for better encapsulation. --- onnxruntime/core/framework/random_seed.cc | 19 +++++++------------ 1 file changed, 7 insertions(+), 12 deletions(-) diff --git a/onnxruntime/core/framework/random_seed.cc b/onnxruntime/core/framework/random_seed.cc index 1951f88c62c65..abf566e2a91cf 100644 --- a/onnxruntime/core/framework/random_seed.cc +++ b/onnxruntime/core/framework/random_seed.cc @@ -9,23 +9,18 @@ namespace onnxruntime { namespace utils { -// "Global initializer calls a non-constexpr function." -// TODO: Fix the warning. The variable should be put in the environment class. -#if defined(_MSC_VER) && !defined(__clang__) -#pragma warning(push) -#pragma warning(disable : 26426) -#endif -static std::atomic g_random_seed(std::chrono::system_clock::now().time_since_epoch().count()); -#if defined(_MSC_VER) && !defined(__clang__) -#pragma warning(pop) -#endif +static std::atomic& GetGlobalRandomSeed() { + static std::atomic g_random_seed( + std::chrono::system_clock::now().time_since_epoch().count()); + return g_random_seed; +} int64_t GetRandomSeed() { - return g_random_seed.load(); + return GetGlobalRandomSeed().load(); } void SetRandomSeed(int64_t seed) { - g_random_seed.store(seed); + GetGlobalRandomSeed().store(seed); // Reset default generators. RandomGenerator::Default().SetSeed(seed);