Skip to content

Commit

Permalink
fix: RandomSeed with 32 bit in Examples (#3860)
Browse files Browse the repository at this point in the history
Right now we have a 64 bit seed but only use a 32 bit RNG. Here I align them by using 32 bit for both.

Discovered in #3849
  • Loading branch information
andiwand authored Nov 14, 2024
1 parent 3aeba0c commit 2f25b25
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 24 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -6,18 +6,8 @@
// License, v. 2.0. If a copy of the MPL was not distributed with this
// file, You can obtain one at https://mozilla.org/MPL/2.0/.

//
// RandomNumbers.hpp
// ActsExamples
//
// Created by Andreas Salzburger on 17/05/16.
//
//

#pragma once

#include "ActsExamples/Framework/AlgorithmContext.hpp"

#include <cstdint>
#include <random>

Expand All @@ -27,6 +17,9 @@ struct AlgorithmContext;
/// The random number generator used in the framework.
using RandomEngine = std::mt19937; ///< Mersenne Twister

/// The seed type used in the framework.
using RandomSeed = std::uint32_t;

/// Provide event and algorithm specific random number generator.s
///
/// This provides local random number generators, allowing for
Expand All @@ -41,7 +34,7 @@ using RandomEngine = std::mt19937; ///< Mersenne Twister
class RandomNumbers {
public:
struct Config {
std::uint64_t seed = 1234567890u; ///< random seed
RandomSeed seed = 1234567890u; ///< random seed
};

explicit RandomNumbers(const Config& cfg);
Expand All @@ -59,7 +52,7 @@ class RandomNumbers {
///
/// This should only be used in special cases e.g. where a custom
/// random engine is used and `spawnGenerator` can not be used.
std::uint64_t generateSeed(const AlgorithmContext& context) const;
RandomSeed generateSeed(const AlgorithmContext& context) const;

private:
Config m_cfg;
Expand Down
19 changes: 7 additions & 12 deletions Examples/Framework/src/Framework/RandomNumbers.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,26 +6,21 @@
// License, v. 2.0. If a copy of the MPL was not distributed with this
// file, You can obtain one at https://mozilla.org/MPL/2.0/.

//
// RandomNumbers.cpp
// ActsExamples
//
// Created by Andreas Salzburger on 17/05/16.
//
//

#include "ActsExamples/Framework/RandomNumbers.hpp"

#include "ActsExamples/Framework/AlgorithmContext.hpp"

ActsExamples::RandomNumbers::RandomNumbers(const Config& cfg) : m_cfg(cfg) {}
namespace ActsExamples {

RandomNumbers::RandomNumbers(const Config& cfg) : m_cfg(cfg) {}

ActsExamples::RandomEngine ActsExamples::RandomNumbers::spawnGenerator(
RandomEngine RandomNumbers::spawnGenerator(
const AlgorithmContext& context) const {
return RandomEngine(generateSeed(context));
}

std::uint64_t ActsExamples::RandomNumbers::generateSeed(
const AlgorithmContext& context) const {
RandomSeed RandomNumbers::generateSeed(const AlgorithmContext& context) const {
return m_cfg.seed + context.eventNumber;
}

} // namespace ActsExamples

0 comments on commit 2f25b25

Please sign in to comment.