CORSIKA  @c8_version@
The framework to simulate particle cascades for astroparticle physics
UniformRealDistribution.hpp
1 /*
2  * (c) Copyright 2020 CORSIKA Project, corsika-project@lists.kit.edu
3  *
4  * This software is distributed under the terms of the GNU General Public
5  * Licence version 3 (GPL Version 3). See file LICENSE for a full version of
6  * the license.
7  */
8 
9 #pragma once
10 
12 #include <random>
13 
14 namespace corsika {
15 
16  template <typename Quantity>
18  static_assert(is_quantity_v<Quantity>, "usable only with Quantity types");
19 
20  typedef typename Quantity::value_type real_type;
21  typedef std::uniform_real_distribution<real_type> distribution_type;
22 
23  public:
24  typedef Quantity value_type;
25 
26  UniformRealDistribution() = delete;
27 
28  UniformRealDistribution(value_type b)
29  : min_{value_type::zero()}
30  , diff_{b} {}
31 
32  UniformRealDistribution(value_type pmin, value_type pmax)
33  : min_(pmin)
34  , diff_{pmax - pmin} {}
35 
37  : min_{other.min_}
38  , diff_{other.diff_} {}
39 
42  if (this == &other) return *this;
43  min_ = other.min_;
44  diff_ = other.diff_;
45  return *this;
46  }
47 
53  value_type getMax() const { return min_ + diff_; }
54 
60  void setMax(value_type const pmax) { diff_ = pmax - min_; }
61 
67  value_type getMin() const { return min_; }
68 
74  void setMin(value_type const pmin) { min_ = pmin; }
75 
83  template <class TGenerator>
84  value_type operator()(TGenerator& g) {
85  return min_ + dist_(g) * diff_;
86  }
87 
88  private:
89  distribution_type dist_{real_type(0.), real_type(1.)};
90 
91  value_type min_;
92  value_type diff_;
93  };
94 
95 } // namespace corsika
value_type getMax() const
Get the upper limit.
value_type operator()(TGenerator &g)
Generate a random number in the range [min, max).
Import and extend the phys::units package.
value_type getMin() const
Get the lower bound.
void setMin(value_type const pmin)
Set the lower bound.
`, since they are used everywhere as integral part of the framework.
void setMax(value_type const pmax)
Set the upper bound.