uniform_rng.hpp
Go to the documentation of this file.
1 #ifndef STAN_MATH_PRIM_SCAL_PROB_UNIFORM_RNG_HPP
2 #define STAN_MATH_PRIM_SCAL_PROB_UNIFORM_RNG_HPP
3 
4 #include <boost/random/uniform_real_distribution.hpp>
5 #include <boost/random/variate_generator.hpp>
14 
15 namespace stan {
16 namespace math {
17 
18 /**
19  * Return a uniform random variate for the given upper and lower bounds using
20  * the specified random number generator.
21  *
22  * alpha and beta can each be a scalar or a one-dimensional container. Any
23  * non-scalar inputs must be the same size.
24  *
25  * @tparam T_alpha Type of shape parameter
26  * @tparam T_beta Type of inverse scale parameter
27  * @tparam RNG type of random number generator
28  * @param alpha (Sequence of) lower bound parameter(s)
29  * @param beta (Sequence of) upper bound parameter(s)
30  * @param rng random number generator
31  * @return (Sequence of) uniform random variate(s)
32  * @throw std::domain_error if alpha or beta are non-finite
33  * @throw std::invalid_argument if non-scalar arguments are of different
34  * sizes
35  */
36 template <typename T_alpha, typename T_beta, class RNG>
38  const T_alpha& alpha, const T_beta& beta, RNG& rng) {
39  using boost::random::uniform_real_distribution;
40  using boost::variate_generator;
41 
42  static const char* function = "uniform_rng";
43 
44  check_finite(function, "Lower bound parameter", alpha);
45  check_finite(function, "Upper bound parameter", beta);
46  check_consistent_sizes(function, "Lower bound parameter", alpha,
47  "Upper bound parameter", beta);
48  check_greater(function, "Upper bound parameter", beta, alpha);
49 
50  scalar_seq_view<T_alpha> alpha_vec(alpha);
51  scalar_seq_view<T_beta> beta_vec(beta);
52  size_t N = max_size(alpha, beta);
54 
55  variate_generator<RNG&, uniform_real_distribution<> > uniform_rng(
56  rng, uniform_real_distribution<>(0.0, 1.0));
57  for (size_t n = 0; n < N; ++n) {
58  output[n] = (beta_vec[n] - alpha_vec[n]) * uniform_rng() + alpha_vec[n];
59  }
60 
61  return output.data();
62 }
63 
64 } // namespace math
65 } // namespace stan
66 #endif
ofstream output
void check_finite(const char *function, const char *name, const T_y &y)
Double_t beta
VectorBuilder< true, double, T_alpha, T_beta >::type uniform_rng(const T_alpha &alpha, const T_beta &beta, RNG &rng)
Definition: uniform_rng.hpp:37
size_t max_size(const T1 &x1, const T2 &x2)
Definition: max_size.hpp:9
void check_greater(const char *function, const char *name, const T_y &y, const T_low &low)
void check_consistent_sizes(const char *function, const char *name1, const T1 &x1, const char *name2, const T2 &x2)