GestureRecognitionToolkit  Version: 0.2.0
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 <cstring>
35 #include "../Util/GRTVersionInfo.h"
36 #include "Timer.h"
37 
38 GRT_BEGIN_NAMESPACE
39 
40 class Random{
41 public:
48  Random(unsigned long long seed = 0):v(4101842887655102017LL), w(1), storedval(0.0){
49  if( seed == 0 ){
50  Timer t;
51  seed = (unsigned long long)t.getSystemTime();
52  }
53  setSeed( seed );
54  }
55 
60  }
61 
68  void setSeed(unsigned long long seed = 0){
69  if( seed == 0 ){
70  Timer t;
71  seed = (unsigned long long)t.getSystemTime();
72  }
73  v = 4101842887655102017LL;
74  w = 1;
75  storedval = 0;
76  u = seed ^ v; int64();
77  v = u; int64();
78  w = v; int64();
79  }
80 
88  inline int getRandomNumberInt(int minRange,int maxRange){
89  return int( floor(getRandomNumberUniform(minRange,maxRange)) );
90  }
91 
105  int getRandomNumberWeighted(const Vector< int > &values,const VectorFloat &weights){
106 
107  if( values.size() != weights.size() ) return 0;
108 
109  unsigned int N = (unsigned int)values.size();
110  Vector< IndexedDouble > weightedValues( N );
111  for(unsigned int i=0; i<N; i++){
112  weightedValues[i].index = values[i];
113  weightedValues[i].value = weights[i];
114  }
115 
116  return getRandomNumberWeighted( weightedValues );
117  }
118 
130 
131  unsigned int N = (unsigned int)weightedValues.size();
132 
133  if( N == 0 ) return 0;
134  if( N == 1 ) return weightedValues[0].index;
135 
136  //Sort the weighted values by value in ascending order (so the least likely value is first, the second most likely is second, etc...
137  sort(weightedValues.begin(),weightedValues.end(),IndexedDouble::sortIndexedDoubleByValueAscending);
138 
139  //Create the accumulated sum lookup table
140  Vector< Float > x(N);
141  x[0] = weightedValues[0].value;
142  for(unsigned int i=1; i<N; i++){
143  x[i] = x[i-1] + weightedValues[i].value;
144  }
145 
146  //Generate a random value between min and the max weighted Float values
147  Float randValue = getRandomNumberUniform(0,x[N-1]);
148 
149  //Find which bin the rand value falls into, return the index of that bin
150  for(unsigned int i=0; i<N; i++){
151  if( randValue <= x[i] ){
152  return weightedValues[ i ].index;
153  }
154  }
155  return 0;
156  }
157 
174 
175  unsigned int N = (unsigned int)weightedValues.size();
176 
177  if( weightedValues.size() != x.size() ) return 0;
178 
179  //Generate a random value between min and the max weighted Float values
180  Float randValue = getRandomNumberUniform(0,x[N-1]);
181 
182  //Find which bin the rand value falls into, return the index of that bin
183  for(unsigned int i=0; i<N; i++){
184  if( randValue <= x[i] ){
185  return weightedValues[ i ].index;
186  }
187  }
188  return 0;
189  }
190 
198  inline Float getRandomNumberUniform(Float minRange=0.0,Float maxRange=1.0){
199  return (doub()*(maxRange-minRange))+minRange;
200  }
201 
209  Float getRandomNumberGauss(Float mu=0.0,Float sigma=1.0){
210  Float v1,v2,rsq,fac;
211 
212  if (storedval == 0.){
213  do {
214  v1=2.0*doub()-1.0;
215  v2=2.0*doub()-1.0;
216  rsq=v1*v1+v2*v2;
217  } while (rsq >= 1.0 || rsq == 0.0);
218  fac=sqrt(-2.0*log(rsq)/rsq);
219  storedval = v1*fac;
220  return mu + sigma*v2*fac;
221  } else {
222  fac = storedval;
223  storedval = 0.;
224  return mu + sigma*fac;
225  }
226  }
227 
236  VectorFloat getRandomVectorUniform(UINT numDimensions,Float minRange=0.0,Float maxRange=1.0){
237  VectorFloat randomValues(numDimensions);
238  for(UINT i=0; i<numDimensions; i++){
239  randomValues[i] = getRandomNumberUniform(minRange,maxRange);
240  }
241  return randomValues;
242  }
243 
252  VectorFloat getRandomVectorGauss(UINT numDimensions,Float mu=0.0,Float sigma=1.0){
253  VectorFloat randomValues(numDimensions);
254  for(UINT i=0; i<numDimensions; i++){
255  randomValues[i] = getRandomNumberGauss(mu,sigma);
256  }
257  return randomValues;
258  }
259 
268  Vector< unsigned int > getRandomSubset( const unsigned int startRange, const unsigned int endRange, const unsigned int subsetSize ){
269 
270  unsigned int i = 0;
271  const unsigned int rangeSize = endRange - startRange;
272 
273  grt_assert( rangeSize > 0 );
274  grt_assert( endRange > startRange );
275  grt_assert( subsetSize <= rangeSize );
276 
277  Vector< unsigned int > indexs( rangeSize );
278  Vector< unsigned int > subset ( subsetSize );
279 
280  //Fill up the range buffer and the randomly suffle it
281  for(i=startRange; i<endRange; i++){
282  indexs[i] = i;
283  }
284  std::random_shuffle(indexs.begin(), indexs.end());
285 
286  //Select the first X values from the randomly shuffled range buffer as the subset
287  for(i=0; i<subsetSize; i++){
288  subset[i] = indexs[i];
289  }
290 
291  return subset;
292  }
293 
294 private:
295  inline unsigned long long int64() {
296  u = u * 2862933555777941757LL + 7046029254386353087LL;
297  v ^= v >> 17; v ^= v << 31; v ^= v >> 8;
298  w = 4294957665U*(w & 0xffffffff) + (w >> 32);
299  unsigned long long x = u ^ (u << 21); x ^= x >> 35; x ^= x << 4;
300  return (x + v) ^ w;
301  }
302  inline Float doub() { return 5.42101086242752217E-20 * int64(); }
303  inline unsigned int int32() { return (unsigned int)int64(); }
304 
305  unsigned long long u;
306  unsigned long long v;
307  unsigned long long w;
308  Float storedval; //This is for the Gauss Box-Muller
309 };
310 
311 GRT_END_NAMESPACE
312 
313 #endif //GRT_RANDOM_HEADER
Definition: Timer.h:43
VectorFloat getRandomVectorGauss(UINT numDimensions, Float mu=0.0, Float sigma=1.0)
Definition: Random.h:252
Definition: Random.h:40
Float getRandomNumberGauss(Float mu=0.0, Float sigma=1.0)
Definition: Random.h:209
Vector< unsigned int > getRandomSubset(const unsigned int startRange, const unsigned int endRange, const unsigned int subsetSize)
Definition: Random.h:268
int getRandomNumberWeighted(Vector< IndexedDouble > &weightedValues, VectorFloat &x)
Definition: Random.h:173
int getRandomNumberWeighted(Vector< IndexedDouble > weightedValues)
Definition: Random.h:129
int getRandomNumberWeighted(const Vector< int > &values, const VectorFloat &weights)
Definition: Random.h:105
Float getRandomNumberUniform(Float minRange=0.0, Float maxRange=1.0)
Definition: Random.h:198
int getRandomNumberInt(int minRange, int maxRange)
Definition: Random.h:88
VectorFloat getRandomVectorUniform(UINT numDimensions, Float minRange=0.0, Float maxRange=1.0)
Definition: Random.h:236
~Random()
Definition: Random.h:59
static unsigned long getSystemTime()
Definition: Timer.h:211
void setSeed(unsigned long long seed=0)
Definition: Random.h:68
Random(unsigned long long seed=0)
Definition: Random.h:48