Vcsn  2.4
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 
16  template <typename RandomGenerator = std::default_random_engine>
17  static bool choose_map(const std::vector<float>& map,
18  RandomGenerator& gen = RandomGenerator())
19  {
20  float sum = std::accumulate(map.begin(), map.end(), 0.0);
21  auto dis = std::bernoulli_distribution(sum);
22  return dis(gen);
23  }
24 
26  template <typename RandomGenerator>
28  {
29  discrete_chooser(RandomGenerator& g)
30  : gen_(g)
31  {}
32 
33  template <typename Container_w, typename Container>
34  typename Container::iterator
35  select(Container_w weight, Container cont) const
36  {
37  auto dis = std::discrete_distribution<>(weight.begin(), weight.end());
38  auto res = std::next(cont.begin(), dis(gen_));
39  return res;
40  }
41 
42  template <typename Container_w, typename Container>
43  typename Container::iterator
44  operator()(Container_w weight, Container cont) const
45  {
46  return select(weight, cont);
47  }
48 
49  private:
50  RandomGenerator& gen_;
51  };
52 
55  template <typename RandomGenerator>
57  {
58  random_selector(RandomGenerator& g)
59  : gen_(g)
60  {}
61 
63  template <typename Iter>
64  Iter select(Iter start, Iter end)
65  {
66  auto dis
67  = std::uniform_int_distribution<>(0, std::distance(start, end) - 1);
68  std::advance(start, dis(gen_));
69  return start;
70  }
71 
73  template <typename Iter>
74  Iter operator()(Iter start, Iter end)
75  {
76  return select(start, end);
77  }
78 
80  template <typename Container>
81  auto operator()(const Container& c) -> decltype(*std::begin(c))
82  {
83  return *select(std::begin(c), std::end(c));
84  }
85 
87  template <typename Container>
88  auto pop(Container& c) -> typename Container::value_type
89  {
90  auto i = select(std::begin(c), std::end(c));
91  auto res = *i;
92  c.erase(i);
93  return res;
94  }
95 
96  private:
97  RandomGenerator& gen_;
98  };
99 
100  template <typename RandomGenerator>
102  make_random_selector(RandomGenerator& g)
103  {
104  return g;
105  }
106 }
return res
Definition: multiply.hh:398
Container::iterator operator()(Container_w weight, Container cont) const
Definition: random.hh:44
auto operator()(const Container &c) -> decltype(*std::begin(c))
A randomly selected member of c.
Definition: random.hh:81
value_impl< detail::weight_tag > weight
Definition: fwd.hh:28
discrete_chooser(RandomGenerator &g)
Definition: random.hh:29
Container::iterator select(Container_w weight, Container cont) const
Definition: random.hh:35
Random selector on container, using uniform distribution.
Definition: random.hh:56
#define LIBVCSN_API
Definition: export.hh:8
Iter operator()(Iter start, Iter end)
A randomly selected iterator in [start, end).
Definition: random.hh:74
std::mt19937 & make_random_engine()
Generate a unique random device.
Definition: random.cc:6
auto pop(Container &c) -> typename Container::value_type
A randomly selected member of c. Remove it from c.
Definition: random.hh:88
random_selector(RandomGenerator &g)
Definition: random.hh:58
Definition: a-star.hh:8
RandomGenerator & gen_
Definition: random.hh:97
random_selector< RandomGenerator > make_random_selector(RandomGenerator &g)
Definition: random.hh:102
static bool choose_map(const std::vector< float > &map, RandomGenerator &gen=RandomGenerator())
Choose whether to pick an element from a map or not.
Definition: random.hh:17
RandomGenerator & gen_
Definition: random.hh:50
Iter select(Iter start, Iter end)
A randomly selected iterator in [start, end).
Definition: random.hh:64
Random selector on container, using discrete distribution.
Definition: random.hh:27