32 #ifndef VSMC_RNG_BERNOULLI_DISTRIBUTION_HPP 33 #define VSMC_RNG_BERNOULLI_DISTRIBUTION_HPP 46 return p >= 0 && p <= 1;
53 template <
typename IntType>
57 Bernoulli, bernoulli, IntType,
double, p, 0.5)
66 template <
typename RNGType>
70 double u = runif(rng);
72 return generate(u, param.
p(),
static_cast<result_type *
>(
nullptr));
75 static bool generate(
double u,
double p,
bool *) {
return u < p; }
78 static U generate(
double u,
double p, U *)
87 template <std::
size_t K,
typename IntType,
typename RNGType>
89 RNGType &rng, std::size_t n, IntType *r,
double p)
93 std::memset(r, 0,
sizeof(IntType) * n);
94 for (std::size_t i = 0; i != n; ++i)
103 template <
typename IntType,
typename RNGType>
105 RNGType &rng, std::size_t n, IntType *r, IntType p)
107 const std::size_t k = 1000;
108 const std::size_t m = n / k;
109 const std::size_t l = n % k;
110 for (std::size_t i = 0; i != m; ++i)
111 internal::bernoulli_distribution_impl<k>(rng, k, r + i * k, p);
112 internal::bernoulli_distribution_impl<k>(rng, l, r + m * k, p);
115 template <
typename IntType,
typename RNGType>
117 std::size_t n, IntType *r)
124 #endif // VSMC_RNG_BERNOULLI_DISTRIBUTION_HPP
Standard uniform distribution with open/closed variants.
#define VSMC_DEFINE_RNG_DISTRIBUTION_1(Name, name, T, T1, p1, v1)
void bernoulli_distribution(RNGType &rng, std::size_t n, IntType *r, IntType p)
Generating bernoulli random variates.
void rng_rand(RNGType &rng, BernoulliDistribution< IntType > &dist, std::size_t n, IntType *r)
#define VSMC_DEFINE_RNG_DISTRIBUTION_OPERATORS
void u01_co_distribution(RNGType &rng, std::size_t n, RealType *r)
Generate standard uniform random variates on closed-open interval.
bool bernoulli_distribution_check_param(double p)
void bernoulli_distribution_impl(RNGType &rng, std::size_t n, IntType *r, double p)