MCKL
Monte Carlo Kernel Library
uniform_int_distribution.hpp
Go to the documentation of this file.
1 //============================================================================
2 // MCKL/include/mckl/random/uniform_int_distribution.hpp
3 //----------------------------------------------------------------------------
4 // MCKL: Monte Carlo Kernel Library
5 //----------------------------------------------------------------------------
6 // Copyright (c) 2013-2018, Yan Zhou
7 // All rights reserved.
8 //
9 // Redistribution and use in source and binary forms, with or without
10 // modification, are permitted provided that the following conditions are met:
11 //
12 // Redistributions of source code must retain the above copyright notice,
13 // this list of conditions and the following disclaimer.
14 //
15 // Redistributions in binary form must reproduce the above copyright notice,
16 // this list of conditions and the following disclaimer in the documentation
17 // and/or other materials provided with the distribution.
18 //
19 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS AS IS
20 // AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21 // IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22 // ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
23 // LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24 // CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25 // SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26 // INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27 // CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28 // ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29 // POSSIBILITY OF SUCH DAMAGE.
30 //============================================================================
31 
32 #ifndef MCKL_RANDOM_UNIFORM_INT_DISTRIBUTION_HPP
33 #define MCKL_RANDOM_UNIFORM_INT_DISTRIBUTION_HPP
34 
37 
38 namespace mckl {
39 
40 namespace internal {
41 
42 template <typename IntType>
44  IntType a, IntType b, std::true_type)
45 {
46  constexpr IntType imax = const_one<IntType>() << 32;
47 
48  return a > -imax && b < imax;
49 }
50 
51 template <typename IntType>
53  IntType, IntType b, std::false_type)
54 {
55  constexpr IntType imax = const_one<IntType>() << 32;
56 
57  return b < imax;
58 }
59 
60 template <typename IntType>
62  IntType, IntType, std::true_type)
63 {
64  return true;
65 }
66 
67 template <typename IntType>
69  IntType a, IntType b, std::false_type)
70 {
72  a, b, std::is_signed<IntType>());
73 }
74 
75 template <typename IntType>
76 inline bool uniform_int_distribution_use_double(IntType a, IntType b)
77 {
79  std::integral_constant<bool,
80  std::numeric_limits<IntType>::digits <= 32>());
81 }
82 
83 template <typename IntType>
84 inline bool uniform_int_distribution_check_param(IntType a, IntType b)
85 {
86  return a <= b;
87 }
88 
89 template <std::size_t K, typename IntType, typename RNGType>
90 inline void uniform_int_distribution_impl(RNGType &rng, std::size_t n,
91  IntType *r, IntType a, IntType b, std::true_type)
92 {
93  alignas(MCKL_ALIGNMENT) std::array<double, K> s;
94  double ra = static_cast<double>(a);
95  double rb = static_cast<double>(b);
96  double *const u = s.data();
97  u01_co_distribution(rng, n, u);
98  muladd(n, u, rb - ra + const_one<double>(), ra, u);
99  floor(n, u, u);
100  for (std::size_t i = 0; i != n; ++i) {
101  r[i] = static_cast<IntType>(u[i]);
102  }
103 }
104 
105 template <std::size_t, typename IntType, typename RNGType>
106 inline void uniform_int_distribution_impl(RNGType &rng, std::size_t n,
107  IntType *r, IntType a, IntType b, std::false_type)
108 {
109  std::uniform_int_distribution<IntType> uniform(a, b);
110  rand(rng, uniform, n, r);
111 }
112 
113 template <std::size_t K, typename IntType, typename RNGType>
115  RNGType &rng, std::size_t n, IntType *r, IntType a, IntType b)
116 {
117  if (a == b) {
118  std::fill_n(r, n, a);
119  return;
120  }
121 
123  uniform_int_distribution_impl<K>(rng, n, r, a, b, std::true_type()) :
124  uniform_int_distribution_impl<K>(rng, n, r, a, b, std::false_type());
125 }
126 
127 } // namespace internal
128 
130  UniformInt, uniform_int, IntType, IntType, a, IntType, b)
131 
132 template <typename IntType>
136 {
138  MCKL_DEFINE_RANDOM_DISTRIBUTION_2(UniformInt, uniform_int, IntType,
139  result_type, a, 0, result_type, b,
140  std::numeric_limits<result_type>::max())
142 
143  public:
144  result_type min() const { return a(); }
145 
146  result_type max() const { return b(); }
147 
148  void reset() {}
149 
150  private:
151  template <typename RNGType>
152  result_type generate(RNGType &rng, const param_type &param)
153  {
154  using UIntType = std::make_unsigned_t<result_type>;
155 
156  constexpr result_type imin = std::numeric_limits<result_type>::min();
157  constexpr result_type imax = std::numeric_limits<result_type>::max();
158 
159  if (param.a() == param.b()) {
160  return param.a();
161  }
162 
163  if (param.a() == imin && param.b() == imax) {
165  union {
166  UIntType u;
167  result_type r;
168  } buf;
169  buf.u = ubits(rng);
170 
171  return buf.r;
172  }
173 
175  param.a(), param.b()) ?
176  generate(rng, param, std::true_type()) :
177  generate(rng, param, std::false_type());
178  }
179 
180  template <typename RNGType>
181  result_type generate(RNGType &rng, const param_type &param, std::true_type)
182  {
184  const double ra = static_cast<double>(param.a());
185  const double rb = static_cast<double>(param.b());
186  const double u = ra + (rb - ra + const_one<double>()) * u01(rng);
187 
188  return static_cast<result_type>(std::floor(u));
189  }
190 
191  template <typename RNGType>
192  result_type generate(
193  RNGType &rng, const param_type &param, std::false_type)
194  {
195  std::uniform_int_distribution<result_type> uniform(
196  param.a(), param.b());
197 
198  return uniform(rng);
199  }
200 }; // class UniformIntDistribution
201 
203 
204 } // namespace mckl
205 
206 #endif // MCKL_RANDOM_UNIFORM_INT_DISTRIBUTION_HPP
#define MCKL_DEFINE_RANDOM_DISTRIBUTION_2( Name, name, T, T1, p1, v1, T2, p2, v2)
Standard uniform distribution on [0, 1)
Uniform integer distribution.
T muladd(T a, T b, T c)
Definition: vmf.hpp:673
#define MCKL_DEFINE_RANDOM_DISTRIBUTION_RAND(Name, T)
RealType u01(UIntType u)
Convert uniform unsigned integers to floating points within [0, 1].
Definition: u01.hpp:88
void floor(std::size_t n, const float *a, float *y)
Definition: vmf.hpp:307
void u01_co_distribution(RNGType &rng, std::size_t n, RealType *r)
bool uniform_int_distribution_check_param(IntType a, IntType b)
Definition: mcmc.hpp:40
void rand(RNGType &rng, ArcsineDistribution< RealType > &distribution, std::size_t N, RealType *r)
#define MCKL_DEFINE_RANDOM_DISTRIBUTION_BATCH_2( Name, name, T, T1, p1, T2, p2)
#define MCKL_ALIGNMENT
The default alignment for scalar type.
Definition: config.h:187
#define MCKL_DEFINE_RANDOM_DISTRIBUTION_MEMBER_0
bool uniform_int_distribution_use_double_big(IntType a, IntType b, std::true_type)
bool uniform_int_distribution_use_double(IntType, IntType, std::true_type)
void uniform_int_distribution_impl(RNGType &rng, std::size_t n, IntType *r, IntType a, IntType b, std::true_type)
#define MCKL_DEFINE_RANDOM_DISTRIBUTION_ASSERT_INT_TYPE(Name, MinBits)