Skip to content

Commit 222d6b2

Browse files
authored
Merge pull request #135 from boostorg/133v2
Improve efficiency of `beta_distribution::operator()`
2 parents 3289126 + 093554f commit 222d6b2

File tree

2 files changed

+6
-3
lines changed

2 files changed

+6
-3
lines changed

include/boost/random/beta_distribution.hpp

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -101,13 +101,16 @@ class beta_distribution {
101101
template<class URNG>
102102
RealType operator()(URNG& urng) const
103103
{
104+
static const auto alpha_dist = gamma_distribution<RealType>(_alpha, RealType(1.0));
105+
static const auto beta_dist = gamma_distribution<RealType>(_beta, RealType(1.0));
106+
104107
RealType a = 0;
105108
RealType b = 0;
106109

107110
do
108111
{
109-
a = gamma_distribution<RealType>(_alpha, RealType(1.0))(urng);
110-
b = gamma_distribution<RealType>(_beta, RealType(1.0))(urng);
112+
a = alpha_dist(urng);
113+
b = beta_dist(urng);
111114
} while (a + b == RealType(0));
112115

113116
return a / (a + b);

include/boost/random/gamma_distribution.hpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -159,7 +159,7 @@ class gamma_distribution
159159
* the gamma distribution.
160160
*/
161161
template<class Engine>
162-
result_type operator()(Engine& eng)
162+
result_type operator()(Engine& eng) const
163163
{
164164
#ifndef BOOST_NO_STDC_NAMESPACE
165165
// allow for Koenig lookup

0 commit comments

Comments
 (0)