Automatic Differentiation
 
Loading...
Searching...
No Matches
discrete_range_rng.hpp
Go to the documentation of this file.
1#ifndef STAN_MATH_PRIM_PROB_DISCRETE_RANGE_RNG_HPP
2#define STAN_MATH_PRIM_PROB_DISCRETE_RANGE_RNG_HPP
3
9#include <boost/random/uniform_int_distribution.hpp>
10#include <boost/random/variate_generator.hpp>
11
12namespace stan {
13namespace math {
14
35template <typename T_lower, typename T_upper, class RNG>
37discrete_range_rng(const T_lower& lower, const T_upper& upper, RNG& rng) {
38 static constexpr const char* function = "discrete_range_rng";
39 using boost::variate_generator;
40 using boost::random::uniform_int_distribution;
41 check_consistent_sizes(function, "Lower bound parameter", lower,
42 "Upper bound parameter", upper);
43 check_greater_or_equal(function, "Upper bound parameter", upper, lower);
44 if (size_zero(upper, lower)) {
45 return {};
46 }
47
48 scalar_seq_view<T_lower> lower_vec(lower);
49 scalar_seq_view<T_upper> upper_vec(upper);
50 size_t N = max_size(lower, upper);
52
53 for (size_t n = 0; n < N; ++n) {
54 variate_generator<RNG&, uniform_int_distribution<>> discrete_range_rng(
55 rng, uniform_int_distribution<>(lower_vec[n], upper_vec[n]));
56
57 output[n] = discrete_range_rng();
58 }
59
60 return output.data();
61}
62
63} // namespace math
64} // namespace stan
65#endif
typename helper::type type
VectorBuilder allocates type T1 values to be used as intermediate values.
scalar_seq_view provides a uniform sequence-like wrapper around either a scalar or a sequence of scal...
VectorBuilder< true, int, T_lower, T_upper >::type discrete_range_rng(const T_lower &lower, const T_upper &upper, RNG &rng)
Return an integer random variate between the given lower and upper bounds (inclusive) using the speci...
bool size_zero(const T &x)
Returns 1 if input is of length 0, returns 0 otherwise.
Definition size_zero.hpp:19
void check_greater_or_equal(const char *function, const char *name, const T_y &y, const T_low &low, Idxs... idxs)
Throw an exception if y is not greater or equal than low.
void check_consistent_sizes(const char *)
Trivial no input case, this function is a no-op.
int64_t max_size(const T1 &x1, const Ts &... xs)
Calculate the size of the largest input.
Definition max_size.hpp:20
The lgamma implementation in stan-math is based on either the reentrant safe lgamma_r implementation ...