LArSoft  v09_90_00
Liquid Argon Software toolkit - https://larsoft.org/
RandomPolicy.h
Go to the documentation of this file.
1 
12 #ifndef NURANDOM_RANDOMUTILS_PROVIDERS_RANDOMPOLICY_H
13 #define NURANDOM_RANDOMUTILS_PROVIDERS_RANDOMPOLICY_H 1
14 
15 // C/C++ standard libraries
16 #include <ostream> // std::endl
17 #include <memory> // std::unique_ptr<>
18 #include <random> // std::uniform_int_distribution, std::default_random_engine
19 #include <chrono> // std::system_clock
20 
21 // From art and its tool chain
23 #include "fhiclcpp/ParameterSet.h"
24 
25 // Some helper classes
27 
28 
29 namespace rndm {
30 
31  namespace details {
32 
42  template <typename SEED>
43  class RandomPolicy: public RandomSeedPolicyBase<SEED> {
44  public:
47  using seed_t = typename base_t::seed_t;
48 
51  RandomPolicy(fhicl::ParameterSet const& pset): base_t("random")
52  { this_t::configure(pset); }
53 
54 
63  virtual void configure(fhicl::ParameterSet const& pset) override;
64 
66  virtual void print(std::ostream& out) const override;
67 
68 
69  private:
70  class RandomImpl {
71  public:
72  RandomImpl(seed_t master_seed, seed_t min_seed, seed_t max_seed):
73  seed(master_seed),
74  generator(master_seed), distribution(min_seed, max_seed)
75  {}
76 
77  seed_t master_seed() const { return seed; }
78  seed_t min() const { return distribution.min(); }
79  seed_t max() const { return distribution.max(); }
81 
82  private:
83  seed_t seed;
84  std::default_random_engine generator;
85  std::uniform_int_distribution<seed_t> distribution;
86  }; // RandomImpl
87 
88  std::unique_ptr<RandomImpl> random_seed;
89 
91  virtual seed_t createSeed(SeedMasterHelper::EngineId const&) override
92  { return (*random_seed)(); }
93 
94  }; // class RandomPolicy<>
95 
96 
97  template <typename SEED>
99  constexpr seed_t MagicMaxSeed = 900000000;
100  seed_t master_seed = 0;
101  if (!pset.get_if_present("masterSeed", master_seed)) {
102  // get the base seed randomly too, from the clock,
103  // and within [1; MagicMaxSeed]
104  master_seed = 1 +
105  std::chrono::system_clock::now().time_since_epoch().count()
106  % MagicMaxSeed;
107  }
108  random_seed.reset(new RandomImpl(master_seed, 1, MagicMaxSeed));
109  } // RandomPolicy<SEED>::configure()
110 
111 
113  template <typename SEED>
114  void RandomPolicy<SEED>::print(std::ostream& out) const {
115  base_t::print(out);
116  out
117  << "\n master seed: " << random_seed->master_seed()
118  << "\n seed within: [ " << random_seed->min()
119  << " ; " << random_seed->max() << " ]"
120  ;
121  } // RandomPolicy<SEED>::print()
122 
123 
124  } // namespace details
125 
126 } // namespace rndm
127 
128 
129 #endif // NURANDOM_RANDOMUTILS_PROVIDERS_RANDOMPOLICY_H
SEED seed_t
type of the random seed
Definition: BasePolicies.h:45
std::unique_ptr< RandomImpl > random_seed
Definition: BasePolicies.h:826
seed_t seed
seed given at construction, for the record
Definition: BasePolicies.h:821
virtual seed_t createSeed(SeedMasterHelper::EngineId const &) override
Extracts a random seed.
Definition: RandomPolicy.h:91
virtual void print(std::ostream &out) const
Prints information on the configuration of this policy.
Definition: BasePolicies.h:76
Implementation of the "random" policy.
Definition: BasePolicies.h:781
RandomImpl(seed_t master_seed, seed_t min_seed, seed_t max_seed)
Definition: RandomPolicy.h:72
Interface for a policy implementation.
Definition: BasePolicies.h:43
Identifier for a engine, made of module name and optional instance name.
Definition: EngineId.h:22
Defines an interface for random seed assignment policies.
virtual void configure(fhicl::ParameterSet const &pset) override
Configure this policy.
Definition: BasePolicies.h:836
std::uniform_int_distribution< seed_t > distribution
flat
Definition: BasePolicies.h:823
std::default_random_engine generator
random engine
Definition: BasePolicies.h:822
std::optional< T > get_if_present(std::string const &key) const
Definition: ParameterSet.h:267
virtual void print(std::ostream &out) const override
Prints the details of the configuration of the random generator.
Definition: BasePolicies.h:852
RandomPolicy(fhicl::ParameterSet const &pset)
Definition: RandomPolicy.h:51