aboutsummaryrefslogtreecommitdiffhomepage
path: root/absl/random/beta_distribution.h
diff options
context:
space:
mode:
Diffstat (limited to 'absl/random/beta_distribution.h')
-rw-r--r--absl/random/beta_distribution.h31
1 files changed, 21 insertions, 10 deletions
diff --git a/absl/random/beta_distribution.h b/absl/random/beta_distribution.h
index e29894f..b09b02f 100644
--- a/absl/random/beta_distribution.h
+++ b/absl/random/beta_distribution.h
@@ -22,9 +22,10 @@
#include <ostream>
#include <type_traits>
-#include "absl/random/internal/distribution_impl.h"
+#include "absl/meta/type_traits.h"
#include "absl/random/internal/fast_uniform_bits.h"
#include "absl/random/internal/fastmath.h"
+#include "absl/random/internal/generate_real.h"
#include "absl/random/internal/iostream_state_saver.h"
namespace absl {
@@ -275,15 +276,21 @@ typename beta_distribution<RealType>::result_type
beta_distribution<RealType>::AlgorithmJoehnk(
URBG& g, // NOLINT(runtime/references)
const param_type& p) {
+ using random_internal::GeneratePositiveTag;
+ using random_internal::GenerateRealFromBits;
+ using real_type =
+ absl::conditional_t<std::is_same<RealType, float>::value, float, double>;
+
// Based on Joehnk, M. D. Erzeugung von betaverteilten und gammaverteilten
// Zufallszahlen. Metrika 8.1 (1964): 5-15.
// This method is described in Knuth, Vol 2 (Third Edition), pp 134.
- using RandU64ToReal = typename random_internal::RandU64ToReal<result_type>;
- using random_internal::PositiveValueT;
+
result_type u, v, x, y, z;
for (;;) {
- u = RandU64ToReal::template Value<PositiveValueT, false>(fast_u64_(g));
- v = RandU64ToReal::template Value<PositiveValueT, false>(fast_u64_(g));
+ u = GenerateRealFromBits<real_type, GeneratePositiveTag, false>(
+ fast_u64_(g));
+ v = GenerateRealFromBits<real_type, GeneratePositiveTag, false>(
+ fast_u64_(g));
// Direct method. std::pow is slow for float, so rely on the optimizer to
// remove the std::pow() path for that case.
@@ -327,12 +334,14 @@ typename beta_distribution<RealType>::result_type
beta_distribution<RealType>::AlgorithmCheng(
URBG& g, // NOLINT(runtime/references)
const param_type& p) {
+ using random_internal::GeneratePositiveTag;
+ using random_internal::GenerateRealFromBits;
+ using real_type =
+ absl::conditional_t<std::is_same<RealType, float>::value, float, double>;
+
// Based on Cheng, Russell CH. Generating beta variates with nonintegral
// shape parameters. Communications of the ACM 21.4 (1978): 317-322.
// (https://dl.acm.org/citation.cfm?id=359482).
- using RandU64ToReal = typename random_internal::RandU64ToReal<result_type>;
- using random_internal::PositiveValueT;
-
static constexpr result_type kLogFour =
result_type(1.3862943611198906188344642429163531361); // log(4)
static constexpr result_type kS =
@@ -341,8 +350,10 @@ beta_distribution<RealType>::AlgorithmCheng(
const bool use_algorithm_ba = (p.method_ == param_type::CHENG_BA);
result_type u1, u2, v, w, z, r, s, t, bw_inv, lhs;
for (;;) {
- u1 = RandU64ToReal::template Value<PositiveValueT, false>(fast_u64_(g));
- u2 = RandU64ToReal::template Value<PositiveValueT, false>(fast_u64_(g));
+ u1 = GenerateRealFromBits<real_type, GeneratePositiveTag, false>(
+ fast_u64_(g));
+ u2 = GenerateRealFromBits<real_type, GeneratePositiveTag, false>(
+ fast_u64_(g));
v = p.y_ * std::log(u1 / (1 - u1));
w = p.a_ * std::exp(v);
bw_inv = result_type(1) / (p.b_ + w);