Feat C++ API
A feature engineering automation tool
rnd.cc
Go to the documentation of this file.
1 /* FEAT
2 copyright 2017 William La Cava
3 license: GNU/GPL v3
4 */
5 
6 #include "rnd.h"
7 
8 namespace FT {
9 
10  namespace Util{
11 
12  Rnd* Rnd::instance = NULL;
13 
15  {
19  //cout << "Max threads are " <<omp_get_max_threads()<<"\n";
20  rg.resize(omp_get_max_threads());
21  }
22 
24  {
25  if (!instance)
26  {
27  instance = new Rnd();
28  }
29 
30  return instance;
31  }
32 
33  void Rnd::destroy()
34  {
35  if (instance)
36  delete instance;
37 
38  instance = NULL;
39  }
40 
41  void Rnd::set_seed(int new_seed)
42  {
46  if (new_seed == -1)
47  /* if seed is -1, choose a random seed. */
48  {
49 
50  int imax = std::numeric_limits<int>::max();
51 
52  std::uniform_int_distribution<> dist(0, imax);
53 
54  this->seed = dist(rg.at(0));
55 
56  this->set_seed(this->seed);
57  }
58  else
59  /* seed first rg with seed, then seed rest with random ints
60  * from rg[0].
61  */
62  {
63  this->seed = new_seed;
64 
65  rg.at(0).seed(this->seed);
66 
67  int imax = std::numeric_limits<int>::max();
68 
69  std::uniform_int_distribution<> dist(0, imax);
70 
71  for (size_t i = 1; i < rg.size(); ++i)
72  rg.at(i).seed(dist(rg.at(0)));
73  }
74  }
75 
76 
77  int Rnd::rnd_int( int lowerLimit, int upperLimit )
78  {
79  std::uniform_int_distribution<> dist( lowerLimit, upperLimit );
80  return dist(rg.at(omp_get_thread_num()));
81  }
82 
83  float Rnd::rnd_flt(float min, float max)
84  {
85  std::uniform_real_distribution<float> dist(min, max);
86  return dist(rg.at(omp_get_thread_num()));
87  }
88 
89  float Rnd::rnd_dbl(float min, float max)
90  {
91  std::uniform_real_distribution<float> dist(min, max);
92  return dist(rg.at(omp_get_thread_num()));
93  }
94 
95  float Rnd::operator()(unsigned i)
96  {
97  return rnd_dbl(0.0,i);
98  }
99 
100  float Rnd::operator()() { return rnd_flt(0.0,1.0); }
101 
102  /*
103  template <class RandomAccessIterator>
104  void Rnd::shuffle (RandomAccessIterator first, RandomAccessIterator last)
105  {
106  for (auto i=(last-first)-1; i>0; --i)
107  {
108  std::uniform_int_distribution<decltype(i)> d(0,i);
109  swap (first.at(i), first[d(rg[omp_get_thread_num()])]);
110  }
111  }*/
112 
113  /*template<typename Iter>
114  Iter Rnd::select_randomly(Iter start, Iter end)
115  {
116  std::uniform_int_distribution<> dis(0, distance(start, end) - 1);
117  advance(start, dis(rg[omp_get_thread_num()]));
118  return start;
119  }*/
120 
121  /*
122  template<typename T>
123  T Rnd::random_choice(const vector<T>& v)
124  {
125  //return a random element of a vector.
126  assert(v.size()>0 && " attemping to return random choice from empty vector");
127  return *select_randomly(v.begin(),v.end());
128  }*/
129 
130  /*
131  template<typename T, typename D>
132  T Rnd::random_choice(const vector<T>& v, const vector<D>& w )
133  {
134  //return a weighted random element of a vector
135 
136  if(w.size() == 0)
137  {
138  cout<<"random_choice() w.size() = 0 Calling random_choice(v)\n";
139  return random_choice(v);
140  }
141  if(w.size() != v.size())
142  {
143  cout<<"WARN! random_choice() w.size() " << w.size() << "!= v.size() "
144  << v.size() << ", Calling random_choice(v)\n";
145  return random_choice(v);
146  }
147  else
148  {
149  assert(v.size() == w.size());
150  std::discrete_distribution<size_t> dis(w.begin(), w.end());
151 
152  return v[dis(rg[omp_get_thread_num()])];
153  }
154  }*/
155 
156  float Rnd::gasdev()
157  //Returns a normally distributed deviate with zero mean and unit variance
158  {
159  float ran = rnd_flt(-1,1);
160  static int iset=0;
161  static float gset;
162  float fac,rsq,v1,v2;
163  if (iset == 0) {// We don't have an extra deviate handy, so
164  do{
165  v1=float(2.0*rnd_flt(-1,1)-1.0); //pick two uniform numbers in the square ex
166  v2=float(2.0*rnd_flt(-1,1)-1.0); //tending from -1 to +1 in each direction,
167  rsq=v1*v1+v2*v2; //see if they are in the unit circle,
168  } while (rsq >= 1.0 || rsq == 0.0); //and if they are not, try again.
169  fac=float(sqrt(-2.0*log(rsq)/rsq));
170  //Now make the Box-Muller transformation to get two normal deviates. Return one and
171  //save the other for next time.
172  gset=v1*fac;
173  iset=1; //Set flag.
174  return v2*fac;
175  }
176  else
177  { //We have an extra deviate handy,
178  iset=0; //so unset the flag,
179  return gset; //and return it.
180  }
181  }
183  }
184 
185 }
Defines a multi-core random number generator and its operators.
Definition: rnd.h:31
float rnd_dbl(float min=0.0, float max=1.0)
Definition: rnd.cc:89
static void destroy()
Definition: rnd.cc:33
int rnd_int(int lowerLimit, int upperLimit)
Definition: rnd.cc:77
int seed
Definition: rnd.h:131
void set_seed(int new_seed)
Definition: rnd.cc:41
vector< std::mt19937 > rg
Definition: rnd.h:127
static Rnd * instance
Definition: rnd.h:129
static Rnd * initRand()
Definition: rnd.cc:23
float gasdev()
Definition: rnd.cc:156
float operator()()
Definition: rnd.cc:100
float rnd_flt(float min=0.0, float max=1.0)
Definition: rnd.cc:83
#define omp_get_max_threads()
Definition: init.h:14
#define omp_get_thread_num()
Definition: init.h:12
main Feat namespace
Definition: data.cc:13
int i
Definition: params.cc:552