diff --git a/corsika/framework/random/ExponentialDistribution.hpp b/corsika/framework/random/ExponentialDistribution.hpp index 68a1531c7515e334fe0bf2768a7c1e806d36e4e5..bb3c133172dfce71f6a468305754577c2bb60ee5 100644 --- a/corsika/framework/random/ExponentialDistribution.hpp +++ b/corsika/framework/random/ExponentialDistribution.hpp @@ -22,6 +22,7 @@ namespace corsika { template <typename Quantity> class ExponentialDistribution { + static_assert(is_quantity_v<Quantity>, "usable only with Quantity types"); typedef typename Quantity::value_type real_type; typedef std::exponential_distribution<real_type> distribution_type; @@ -31,7 +32,7 @@ namespace corsika { ExponentialDistribution() = delete; - ExponentialDistribution(value_type const& beta) + ExponentialDistribution(value_type beta) : beta_(beta) {} ExponentialDistribution(ExponentialDistribution<value_type> const& other) diff --git a/corsika/framework/random/UniformRealDistribution.hpp b/corsika/framework/random/UniformRealDistribution.hpp index de3b0914de6c3a9737b903fc038b288d5a9e6783..c5251e3a0d3bae98f1b33a348889b548d14fd5ad 100644 --- a/corsika/framework/random/UniformRealDistribution.hpp +++ b/corsika/framework/random/UniformRealDistribution.hpp @@ -15,6 +15,7 @@ namespace corsika { template <typename Quantity> class UniformRealDistribution { + static_assert(is_quantity_v<Quantity>, "usable only with Quantity types"); typedef typename Quantity::value_type real_type; typedef std::uniform_real_distribution<real_type> distribution_type; @@ -24,23 +25,23 @@ namespace corsika { UniformRealDistribution() = delete; - UniformRealDistribution(Quantity const& b) - : min_{value_type(phys::units::detail::magnitude_tag, 0)} - , max_(b) {} + UniformRealDistribution(value_type b) + : min_{value_type::zero()} + , diff_{b} {} - UniformRealDistribution(value_type const& pmin, value_type const& pmax) + UniformRealDistribution(value_type pmin, value_type pmax) : min_(pmin) - , max_(pmax) {} + , diff_{pmax - pmin} {} UniformRealDistribution(UniformRealDistribution<value_type> const& other) - : min_(other.getMin()) - , max_(other.getMax()) {} + : min_{other.min_} + , diff_{other.diff_} {} UniformRealDistribution<value_type>& operator=( UniformRealDistribution<value_type> const& other) { if (this == &other) return *this; - min_ = other.getMin(); - max_ = other.getMax(); + min_ = other.min_; + diff_ = other.diff_; return *this; } @@ -49,31 +50,31 @@ namespace corsika { * * @return value_type */ - value_type getMax() const { return max_; } + value_type getMax() const { return min_ + diff_; } /** - * Set the upper limit. + * Set the upper bound. * - * @param vMax + * @param pmax: new upper bound */ - void setMax(value_type const& pmax) { max_ = pmax; } + void setMax(value_type const pmax) { diff_ = pmax - min_; } /** - * Get the lower limit. + * Get the lower bound. * * @return The minimum possible value. */ value_type getMin() const { return min_; } /** - * Set the lower limit. + * Set the lower bound. * * @param pmin is the new lower bound. */ - void setMin(value_type const& pmin) { min_ = pmin; } + void setMin(value_type const pmin) { min_ = pmin; } /** - * Generate a random number in the range [min, max]. + * Generate a random number in the range [min, max). * * @tparam TGenerator * @param g @@ -81,14 +82,14 @@ namespace corsika { */ template <class TGenerator> value_type operator()(TGenerator& g) { - return min_ + dist_(g) * (max_ - min_); + return min_ + dist_(g) * diff_; } private: distribution_type dist_{real_type(0.), real_type(1.)}; value_type min_; - value_type max_; + value_type diff_; }; } // namespace corsika