Vcsn  2.3
Be Rational
random.hh
Go to the documentation of this file.
1 #pragma once
2 
3 #include <iterator>
4 #include <random>
5 
6 #include <vcsn/misc/export.hh>
7 
8 namespace vcsn LIBVCSN_API
9 {
11  std::mt19937& make_random_engine();
12 
14  template <typename RandomGenerator>
16  {
17  discrete_chooser(RandomGenerator& g)
18  : gen_(g)
19  {}
20 
21  template <typename Iter_weight, typename Iter>
22  Iter select(Iter_weight start_w, Iter_weight end_w, Iter start)
23  {
24  auto dis = std::discrete_distribution<>(start_w, end_w);
25  std::advance(start, dis(gen_));
26  return start;
27  }
28 
29  template <typename Iter_weight, typename Iter>
30  Iter operator()(Iter_weight start_w, Iter_weight end_w, Iter start)
31  {
32  return select(start_w, end_w, start);
33  }
34 
35  private:
36  RandomGenerator& gen_;
37  };
38 
41  template <typename RandomGenerator>
43  {
44  random_selector(RandomGenerator& g)
45  : gen_(g)
46  {}
47 
49  template <typename Iter>
50  Iter select(Iter start, Iter end)
51  {
52  auto dis
53  = std::uniform_int_distribution<>(0, std::distance(start, end) - 1);
54  std::advance(start, dis(gen_));
55  return start;
56  }
57 
59  template <typename Iter>
60  Iter operator()(Iter start, Iter end)
61  {
62  return select(start, end);
63  }
64 
66  template <typename Container>
67  auto operator()(const Container& c) -> decltype(*std::begin(c))
68  {
69  return *select(std::begin(c), std::end(c));
70  }
71 
73  template <typename Container>
74  auto pop(Container& c) -> typename Container::value_type
75  {
76  auto i = select(std::begin(c), std::end(c));
77  auto res = *i;
78  c.erase(i);
79  return res;
80  }
81 
82  private:
83  RandomGenerator& gen_;
84  };
85 
86  template <typename RandomGenerator>
88  make_random_selector(RandomGenerator& g)
89  {
90  return g;
91  }
92 }
Iter select(Iter_weight start_w, Iter_weight end_w, Iter start)
Definition: random.hh:22
Iter operator()(Iter_weight start_w, Iter_weight end_w, Iter start)
Definition: random.hh:30
std::mt19937 & make_random_engine()
Generate a unique random device.
Definition: random.cc:6
RandomGenerator & gen_
Definition: random.hh:83
random_selector(RandomGenerator &g)
Definition: random.hh:44
Random selector on container, using discrete distribution.
Definition: random.hh:15
return res
Definition: multiply.hh:398
Iter select(Iter start, Iter end)
A randomly selected iterator in [start, end).
Definition: random.hh:50
Definition: a-star.hh:8
discrete_chooser(RandomGenerator &g)
Definition: random.hh:17
#define LIBVCSN_API
Definition: export.hh:8
Random selector on container, using uniform distribution.
Definition: random.hh:42
auto operator()(const Container &c) -> decltype(*std::begin(c))
A randomly selected member of c.
Definition: random.hh:67
auto pop(Container &c) -> typename Container::value_type
A randomly selected member of c. Remove it from c.
Definition: random.hh:74
random_selector< RandomGenerator > make_random_selector(RandomGenerator &g)
Definition: random.hh:88
Iter operator()(Iter start, Iter end)
A randomly selected iterator in [start, end).
Definition: random.hh:60
RandomGenerator & gen_
Definition: random.hh:36