GestureRecognitionToolkit  Version: 1.0 Revision: 04-03-15
The Gesture Recognition Toolkit (GRT) is a cross-platform, open-source, c++ machine learning library for real-time gesture recognition.
Random.h
Go to the documentation of this file.
1 
10 /*
11  GRT MIT License
12  Copyright (c) <2012> <Nicholas Gillian, Media Lab, MIT>
13 
14  Permission is hereby granted, free of charge, to any person obtaining a copy of this software
15  and associated documentation files (the "Software"), to deal in the Software without restriction,
16  including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense,
17  and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so,
18  subject to the following conditions:
19 
20  The above copyright notice and this permission notice shall be included in all copies or substantial
21  portions of the Software.
22 
23  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT
24  LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
25  IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
26  WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
27  SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
28  */
29 
30 #ifndef GRT_RANDOM_HEADER
31 #define GRT_RANDOM_HEADER
32 
33 #include <assert.h>
34 #include "../Util/GRTVersionInfo.h"
35 #include "Timer.h"
36 
37 namespace GRT{
38 
39 class Random{
40 public:
47  Random(unsigned long long seed = 0):v(4101842887655102017LL), w(1), storedval(0.0){
48  if( seed == 0 ){
49  Timer t;
50  seed = (unsigned long long)t.getSystemTime();
51  }
52  setSeed( seed );
53  }
54 
59  }
60 
67  void setSeed(unsigned long long seed = 0){
68  if( seed == 0 ){
69  Timer t;
70  seed = (unsigned long long)t.getSystemTime();
71  }
72  v = 4101842887655102017LL;
73  w = 1;
74  storedval = 0;
75  u = seed ^ v; int64();
76  v = u; int64();
77  w = v; int64();
78  }
79 
87  inline int getRandomNumberInt(int minRange,int maxRange){
88  return int( floor(getRandomNumberUniform(minRange,maxRange)) );
89  }
90 
104  int getRandomNumberWeighted(const vector< int > &values,const vector< double > &weights){
105 
106  if( values.size() != weights.size() ) return 0;
107 
108  unsigned int N = (unsigned int)values.size();
109  vector< IndexedDouble > weightedValues( N );
110  for(unsigned int i=0; i<N; i++){
111  weightedValues[i].index = values[i];
112  weightedValues[i].value = weights[i];
113  }
114 
115  return getRandomNumberWeighted( weightedValues );
116  }
117 
128  int getRandomNumberWeighted(vector< IndexedDouble > weightedValues){
129 
130  unsigned int N = (unsigned int)weightedValues.size();
131 
132  if( N == 0 ) return 0;
133  if( N == 1 ) return weightedValues[0].index;
134 
135  //Sort the weighted values by value in ascending order (so the least likely value is first, the second most likely is second, etc...
136  sort(weightedValues.begin(),weightedValues.end(),IndexedDouble::sortIndexedDoubleByValueAscending);
137 
138  //Create the accumulated sum lookup table
139  vector< double > x(N);
140  x[0] = weightedValues[0].value;
141  for(unsigned int i=1; i<N; i++){
142  x[i] = x[i-1] + weightedValues[i].value;
143  }
144 
145  //Generate a random value between min and the max weighted double values
146  double randValue = getRandomNumberUniform(0,x[N-1]);
147 
148  //Find which bin the rand value falls into, return the index of that bin
149  for(unsigned int i=0; i<N; i++){
150  if( randValue <= x[i] ){
151  return weightedValues[ i ].index;
152  }
153  }
154  return 0;
155  }
156 
172  int getRandomNumberWeighted(vector< IndexedDouble > &weightedValues, vector< double > &x){
173 
174  unsigned int N = (unsigned int)weightedValues.size();
175 
176  if( weightedValues.size() != x.size() ) return 0;
177 
178  //Generate a random value between min and the max weighted double values
179  double randValue = getRandomNumberUniform(0,x[N-1]);
180 
181  //Find which bin the rand value falls into, return the index of that bin
182  for(unsigned int i=0; i<N; i++){
183  if( randValue <= x[i] ){
184  return weightedValues[ i ].index;
185  }
186  }
187  return 0;
188  }
189 
197  inline double getRandomNumberUniform(double minRange=0.0,double maxRange=1.0){
198  return (doub()*(maxRange-minRange))+minRange;
199  }
200 
208  double getRandomNumberGauss(double mu=0.0,double sigma=1.0){
209  double v1,v2,rsq,fac;
210 
211  if (storedval == 0.){
212  do {
213  v1=2.0*doub()-1.0;
214  v2=2.0*doub()-1.0;
215  rsq=v1*v1+v2*v2;
216  } while (rsq >= 1.0 || rsq == 0.0);
217  fac=sqrt(-2.0*log(rsq)/rsq);
218  storedval = v1*fac;
219  return mu + sigma*v2*fac;
220  } else {
221  fac = storedval;
222  storedval = 0.;
223  return mu + sigma*fac;
224  }
225  }
226 
235  VectorDouble getRandomVectorUniform(UINT numDimensions,double minRange=0.0,double maxRange=1.0){
236  VectorDouble randomValues(numDimensions);
237  for(UINT i=0; i<numDimensions; i++){
238  randomValues[i] = getRandomNumberUniform(minRange,maxRange);
239  }
240  return randomValues;
241  }
242 
251  VectorDouble getRandomVectorGauss(UINT numDimensions,double mu=0.0,double sigma=1.0){
252  VectorDouble randomValues(numDimensions);
253  for(UINT i=0; i<numDimensions; i++){
254  randomValues[i] = getRandomNumberGauss(mu,sigma);
255  }
256  return randomValues;
257  }
258 
267  std::vector< unsigned int > getRandomSubset( const unsigned int startRange, const unsigned int endRange, const unsigned int subsetSize ){
268 
269  unsigned int i = 0;
270  const unsigned int rangeSize = endRange - startRange;
271 
272  assert( rangeSize > 0 );
273  assert( endRange > startRange );
274  assert( subsetSize <= rangeSize );
275 
276  std::vector< unsigned int > indexs( rangeSize );
277  std::vector< unsigned int > subset ( subsetSize );
278 
279  //Fill up the range buffer and the randomly suffle it
280  for(i=startRange; i<endRange; i++){
281  indexs[i] = i;
282  }
283  std::random_shuffle(indexs.begin(), indexs.end());
284 
285  //Select the first X values from the randomly shuffled range buffer as the subset
286  for(i=0; i<subsetSize; i++){
287  subset[i] = indexs[i];
288  }
289 
290  return subset;
291  }
292 
293 private:
294  inline unsigned long long int64() {
295  u = u * 2862933555777941757LL + 7046029254386353087LL;
296  v ^= v >> 17; v ^= v << 31; v ^= v >> 8;
297  w = 4294957665U*(w & 0xffffffff) + (w >> 32);
298  unsigned long long x = u ^ (u << 21); x ^= x >> 35; x ^= x << 4;
299  return (x + v) ^ w;
300  }
301  inline double doub() { return 5.42101086242752217E-20 * int64(); }
302  inline unsigned int int32() { return (unsigned int)int64(); }
303 
304  unsigned long long u;
305  unsigned long long v;
306  unsigned long long w;
307  double storedval; //This is for the Gauss Box-Muller
308 };
309 
310 }; //End of namespace GRT
311 
312 #endif //GRT_RANDOM_HEADER
VectorDouble getRandomVectorGauss(UINT numDimensions, double mu=0.0, double sigma=1.0)
Definition: Random.h:251
double getRandomNumberGauss(double mu=0.0, double sigma=1.0)
Definition: Random.h:208
Random(unsigned long long seed=0)
Definition: Random.h:47
VectorDouble getRandomVectorUniform(UINT numDimensions, double minRange=0.0, double maxRange=1.0)
Definition: Random.h:235
static unsigned long getSystemTime()
Definition: Timer.h:165
int getRandomNumberWeighted(vector< IndexedDouble > &weightedValues, vector< double > &x)
Definition: Random.h:172
Definition: AdaBoost.cpp:25
std::vector< unsigned int > getRandomSubset(const unsigned int startRange, const unsigned int endRange, const unsigned int subsetSize)
Definition: Random.h:267
~Random()
Definition: Random.h:58
void setSeed(unsigned long long seed=0)
Definition: Random.h:67
int getRandomNumberWeighted(const vector< int > &values, const vector< double > &weights)
Definition: Random.h:104
int getRandomNumberWeighted(vector< IndexedDouble > weightedValues)
Definition: Random.h:128
int getRandomNumberInt(int minRange, int maxRange)
Definition: Random.h:87
double getRandomNumberUniform(double minRange=0.0, double maxRange=1.0)
Definition: Random.h:197