SigUtil  0.95
Utility modules for modern C++
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
random.hpp
Go to the documentation of this file.
1 /*
2 Copyright© 2014 Akihiro Nishimura
3 
4 This software is released under the MIT License.
5 http://opensource.org/licenses/mit-license.php
6 */
7 
8 #ifndef SIG_UTIL_RANDOM_HPP
9 #define SIG_UTIL_RANDOM_HPP
10 
11 #include "../helper/container_traits.hpp"
12 #include <random>
13 
14 
16 
17 namespace sig
18 {
20 
30 template <class T, class Engine = std::mt19937>
32 {
33  mutable Engine engine_; // 乱数生成アルゴリズム
34 
35  mutable typename std::conditional <
36  std::is_integral<T>::value,
37  std::uniform_int_distribution<T>,
38  std::uniform_real_distribution<T>
39  > ::type dist_; // 一様分布
40 
41 public:
43 
48  SimpleRandom(T min, T max, bool debug) : engine_(
49  [debug](){
50  std::random_device rnd;
51  std::vector<unsigned long> v(10);
52  if (debug) std::fill(v.begin(), v.end(), 0);
53  else std::generate(v.begin(), v.end(), std::ref(rnd));
54  std::seed_seq seq(v.begin(), v.end());
55 
56  return Engine{seq};
57  }()
58  ),
59  dist_(min, max){}
60 
62  T operator()() const
63  {
64  return dist_(engine_);
65  }
66 };
67 
68 
70 
84 template <class C = std::vector<int>>
85 C make_unique_numbers(uint n, int min, int max, bool debug)
86 {
87  std::unordered_set<int> match;
88  C result = impl::container_traits<C>::make(n);
89  SimpleRandom<int> Rand(0, max - min, debug);
90 
91  int r;
92  for (uint i = 0; i < n; ++i){
93  do{
94  r = min + Rand();
95  } while (match.find(r) != match.end());
96 
97  match.insert(r);
98  result.push_back(r);
99  }
100 
101  return result;
102 }
103 
104 }
105 
106 #endif
C make_unique_numbers(uint n, int min, int max, bool debug)
重複の無い一様分布の整数乱数を生成
Definition: random.hpp:85
SimpleRandom(T min, T max, bool debug)
コンストラクタ
Definition: random.hpp:48
初期化時に指定した範囲の一様分布乱数を発生させるクラス
Definition: random.hpp:31
T operator()() const
乱数を発生させて取得
Definition: random.hpp:62
auto min(T v) -> T
auto max(T v) -> T
Definition: array.hpp:15