• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // Copyright 2019 Hans Dembinski
2 //
3 // Distributed under the Boost Software License, Version 1.0.
4 // (See accompanying file LICENSE_1_0.txt
5 // or copy at http://www.boost.org/LICENSE_1_0.txt)
6 
7 #include <array>
8 #include <random>
9 
10 using uniform = std::uniform_real_distribution<>;
11 using uniform_int = std::uniform_int_distribution<>;
12 using normal = std::normal_distribution<>;
13 
14 template <class Distribution, class... Ts>
15 Distribution init(Ts...);
16 
17 template <>
init()18 uniform init<uniform>() {
19   return uniform{0.0, 1.0};
20 }
21 
22 template <>
init()23 normal init<normal>() {
24   return normal{0.5, 0.3};
25 }
26 
27 template <>
init(int n)28 uniform_int init<uniform_int, int>(int n) {
29   return uniform_int{0, n};
30 }
31 
32 template <class Distribution, std::size_t N = 1 << 15>
33 struct generator : std::array<double, N> {
34   using base_t = std::array<double, N>;
35 
36   template <class... Ts>
generatorgenerator37   generator(Ts... ts) {
38     std::default_random_engine rng(1);
39     auto dis = init<Distribution>(ts...);
40     std::generate(base_t::begin(), base_t::end(), [&] { return dis(rng); });
41   }
42 
operator ()generator43   const double& operator()() {
44     ++ptr_;
45     if (ptr_ == base_t::data() + N) ptr_ = base_t::data();
46     return *ptr_;
47   }
48 
49   const double* ptr_ = base_t::data() - 1;
50 };
51