pixled-lib  1.0
random.h
1 #ifndef PIXLED_FUNCTIONNAL_RANDOM_H
2 #define PIXLED_FUNCTIONNAL_RANDOM_H
3 
4 #include <random>
5 #include <unordered_map>
6 
7 #include "../function.h"
8 
9 namespace pixled {
13  typedef std::minstd_rand random_engine;
14 }
15 
16 namespace pixled { namespace random {
30  protected:
41  unsigned long seed;
42 
43  public:
54  : period(period) {
55  std::random_device rd_seeder;
56  std::uniform_int_distribution<unsigned long> rd_seed;
57  seed = rd_seed(rd_seeder);
58  }
66  : period(period), seed(seed) {}
67  };
68 
75  class RandomT : public base::Function<random_engine>, RandomEngineConfig {
76  private:
77  mutable time current_period = 0;
78  mutable random_engine rd {this->seed};
79  public:
81 
82  random_engine operator()(led l, time t) const override;
83 
84  RandomT* copy() const override {
85  return new RandomT(period, seed);
86  }
87  };
88 
95  class RandomXYT : public base::Function<random_engine>, RandomEngineConfig {
96  private:
97  mutable time current_period = 0;
98  mutable random_engine rd_seeder {this->seed};
99  mutable std::uniform_int_distribution<unsigned long> rd_seed;
100  mutable std::unordered_map<index_t, random_engine> rds;
101  public:
103 
104  /*
105  * f = period
106  */
107  random_engine operator()(led l, time t) const override;
108 
109  RandomXYT* copy() const override {
110  return new RandomXYT(period, seed);
111  }
112  };
113 
124  template<typename R>
126  public Function<UniformDistribution<R>, R, R, R, random_engine> {
127  public:
129 
130  /*
131  * f1 = min
132  * f2 = max
133  * f3 = random engine
134  */
135  R operator()(led l, time t) const override {
136  std::uniform_real_distribution<float> random_real (
137  this->template call<0>(l, t), this->template call<1>(l, t)
138  );
139  auto engine = this->template call<2>(l, t);
140  return random_real(engine);
141  }
142  };
152  template<typename R>
154  public:
155  using Function<NormalDistribution<R>, R, float, float, std::minstd_rand>::Function;
156 
157  /*
158  * f1 = min
159  * f2 = max
160  * f3 = random engine
161  */
162  R operator()(led l, time t) const override {
163  std::normal_distribution<float> random_real (
164  this->template call<0>(l, t), this->template call<1>(l, t)
165  );
166  auto engine = this->template call<2>(l, t);
167  return random_real(engine);
168  }
169  };
170 }}
171 #endif
Definition: function.h:349
Definition: function.h:33
Definition: random.h:153
R operator()(led l, time t) const override
Definition: random.h:162
Definition: random.h:29
time period
Definition: random.h:37
unsigned long seed
Definition: random.h:41
RandomEngineConfig(time period, unsigned long seed)
Definition: random.h:65
RandomEngineConfig(time period)
Definition: random.h:53
Definition: random.h:75
random_engine operator()(led l, time t) const override
Definition: random.cpp:5
RandomT * copy() const override
Definition: random.h:84
Definition: random.h:95
RandomXYT * copy() const override
Definition: random.h:109
random_engine operator()(led l, time t) const override
Definition: random.cpp:14
Definition: random.h:126
R operator()(led l, time t) const override
Definition: random.h:135
Definition: animation.cpp:3
std::minstd_rand random_engine
Definition: random.h:13
unsigned long time
Definition: time.h:10
Definition: mapping.h:19