-
Notifications
You must be signed in to change notification settings - Fork 31
/
UniformRealDistribution.hh
107 lines (91 loc) · 3.23 KB
/
UniformRealDistribution.hh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
//----------------------------------*-C++-*----------------------------------//
// Copyright 2020-2024 UT-Battelle, LLC, and other Celeritas developers.
// See the top-level COPYRIGHT file for details.
// SPDX-License-Identifier: (Apache-2.0 OR MIT)
//---------------------------------------------------------------------------//
//! \file celeritas/random/distribution/UniformRealDistribution.hh
//---------------------------------------------------------------------------//
#pragma once
#include <cmath>
#include <type_traits>
#include "corecel/Assert.hh"
#include "corecel/Macros.hh"
#include "corecel/Types.hh"
#include "GenerateCanonical.hh"
namespace celeritas
{
//---------------------------------------------------------------------------//
/*!
* Sample from a uniform distribution.
*
* This distribution is defined between two arbitrary real numbers \em a and
* \em b , and has a flat PDF between the two values. It *is* allowable for the
* two numbers to have reversed order.
*/
template<class RealType = ::celeritas::real_type>
class UniformRealDistribution
{
static_assert(std::is_floating_point_v<RealType>);
public:
//!@{
//! \name Type aliases
using real_type = RealType;
using result_type = real_type;
//!@}
public:
// Construct on [0, 1)
inline CELER_FUNCTION UniformRealDistribution();
// Construct on an arbitrary interval
inline CELER_FUNCTION UniformRealDistribution(real_type a, real_type b);
// Sample a random number according to the distribution
template<class Generator>
inline CELER_FUNCTION result_type operator()(Generator& rng);
//// ACCESSORS ////
//! Get the lower bound of the distribution
CELER_FUNCTION real_type a() const { return a_; }
//! Get the upper bound of the distribution
CELER_FUNCTION real_type b() const { return delta_ + a_; }
private:
RealType a_;
RealType delta_;
};
//---------------------------------------------------------------------------//
// INLINE DEFINITIONS
//---------------------------------------------------------------------------//
/*!
* Construct on the interval [0, 1).
*
* This constructor is generally unused because it's simpler and more efficient
* to directly call ``generate_canonical``. We leave it for compatibility with
* the standard.
*/
template<class RealType>
CELER_FUNCTION UniformRealDistribution<RealType>::UniformRealDistribution()
: UniformRealDistribution(0, 1)
{
}
//---------------------------------------------------------------------------//
/*!
* Construct on the interval [a, b).
*/
template<class RealType>
CELER_FUNCTION
UniformRealDistribution<RealType>::UniformRealDistribution(real_type a,
real_type b)
: a_(a), delta_(b - a)
{
CELER_EXPECT(a <= b);
}
//---------------------------------------------------------------------------//
/*!
* Sample a random number according to the distribution.
*/
template<class RealType>
template<class Generator>
CELER_FUNCTION auto
UniformRealDistribution<RealType>::operator()(Generator& rng) -> result_type
{
return std::fma(delta_, generate_canonical<RealType>(rng), a_);
}
//---------------------------------------------------------------------------//
} // namespace celeritas