GestureRecognitionToolkit  Version: 0.2.5
The Gesture Recognition Toolkit (GRT) is a cross-platform, open-source, c++ machine learning library for real-time gesture recognition.
Random.cpp
1 /*
2  GRT MIT License
3  Copyright (c) <2012> <Nicholas Gillian, Media Lab, MIT>
4 
5  Permission is hereby granted, free of charge, to any person obtaining a copy of this software
6  and associated documentation files (the "Software"), to deal in the Software without restriction,
7  including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense,
8  and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so,
9  subject to the following conditions:
10 
11  The above copyright notice and this permission notice shall be included in all copies or substantial
12  portions of the Software.
13 
14  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT
15  LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
16  IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
17  WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
18  SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
19  */
20 
21 #define GRT_DLL_EXPORTS
22 #include "Random.h"
23 
24 GRT_BEGIN_NAMESPACE
25 
26 #if GRT_USE_CXX11_RANDOM_ALGO
27 Random::Random( ) : uniformRealDistribution(0.0,1.0), normalDistribution(0.0,1.0){}
28 #else
29 Random::Random(unsigned long long seed):v(4101842887655102017LL), w(1), storedval(0.0){
30  if( seed == 0 ){
31  Timer t;
32  seed = (unsigned long long)t.getSystemTime();
33  }
34  setSeed( seed );
35 }
36 #endif
37 
39 
40 bool Random::setSeed(const unsigned long long seed_){
41 #if GRT_USE_CXX11_RANDOM_ALGO
42  generator.seed( seed_ );
43 #else
44  unsigned long long seed = seed_;
45  if( seed == 0 ){
46  Timer t;
47  seed = (unsigned long long)t.getSystemTime();
48  }
49  v = 4101842887655102017LL;
50  w = 1;
51  storedval = 0;
52  u = seed ^ v; int64();
53  v = u; int64();
54  w = v; int64();
55 #endif
56  return true;
57 }
58 
59 int Random::getRandomNumberInt(int minRange,int maxRange){
60 #if GRT_USE_CXX11_RANDOM_ALGO
61  std::uniform_int_distribution< int > uniformIntDistribution(minRange, maxRange-1);
62  return uniformIntDistribution( generator );
63 #else
64  return int( floor(getRandomNumberUniform(minRange,maxRange)) );
65 #endif
66 }
67 
68 int Random::getRandomNumberWeighted(const Vector< int > &values,const VectorFloat &weights){
69 
70  if( values.size() != weights.size() ) return 0;
71 
72  unsigned int N = (unsigned int)values.size();
73  Vector< IndexedDouble > weightedValues( N );
74  for(unsigned int i=0; i<N; i++){
75  weightedValues[i].index = values[i];
76  weightedValues[i].value = weights[i];
77  }
78 
79  return getRandomNumberWeighted( weightedValues );
80 }
81 
83 
84  unsigned int N = (unsigned int)weightedValues.size();
85 
86  if( N == 0 ) return 0;
87  if( N == 1 ) return weightedValues[0].index;
88 
89  //Sort the weighted values by value in ascending order (so the least likely value is first, the second most likely is second, etc...
90  sort(weightedValues.begin(),weightedValues.end(),IndexedDouble::sortIndexedDoubleByValueAscending);
91 
92  //Create the accumulated sum lookup table
93  Vector< Float > x(N);
94  x[0] = weightedValues[0].value;
95  for(unsigned int i=1; i<N; i++){
96  x[i] = x[i-1] + weightedValues[i].value;
97  }
98 
99  //Generate a random value between min and the max weighted Float values
100  Float randValue = getUniform(0.0,x[N-1]);
101 
102  //Find which bin the rand value falls into, return the index of that bin
103  for(unsigned int i=0; i<N; i++){
104  if( randValue <= x[i] ){
105  return weightedValues[ i ].index;
106  }
107  }
108  return 0;
109 }
110 
112 
113  unsigned int N = (unsigned int)weightedValues.size();
114 
115  if( weightedValues.size() != x.size() ) return 0;
116 
117  //Generate a random value between min and the max weighted Float values
118  Float randValue = getUniform(0.0,x[N-1]);
119 
120  //Find which bin the rand value falls into, return the index of that bin
121  for(unsigned int i=0; i<N; i++){
122  if( randValue <= x[i] ){
123  return weightedValues[ i ].index;
124  }
125  }
126  return 0;
127 }
128 
129 Float Random::getRandomNumberUniform(Float minRange,Float maxRange){
130  return getUniform(minRange,maxRange);
131 }
132 
133 Float Random::getUniform(const Float &minRange,const Float &maxRange){
134 #if GRT_USE_CXX11_RANDOM_ALGO
135  Float r = uniformRealDistribution( generator );
136 #else
137  Float r = doub();
138 #endif
139  return (r*(maxRange-minRange))+minRange;
140 }
141 
142 Float Random::getRandomNumberGauss(Float mu,Float sigma){
143  return getGauss( mu, sigma );
144 }
145 
146 Float Random::getGauss(const Float &mu,const Float &sigma){
147 #if GRT_USE_CXX11_RANDOM_ALGO
148  return mu + (normalDistribution( generator )*sigma);
149 #else
150  Float v1,v2,rsq,fac;
151 
152  if (storedval == 0.){
153  do {
154  v1=2.0*doub()-1.0;
155  v2=2.0*doub()-1.0;
156  rsq=v1*v1+v2*v2;
157  } while (rsq >= 1.0 || rsq == 0.0);
158  fac=sqrt(-2.0*log(rsq)/rsq);
159  storedval = v1*fac;
160  return mu + sigma*v2*fac;
161  } else {
162  fac = storedval;
163  storedval = 0.;
164  return mu + sigma*fac;
165  }
166 #endif
167 }
168 
169 VectorFloat Random::getRandomVectorUniform(UINT numDimensions,Float minRange,Float maxRange){
170  VectorFloat randomValues(numDimensions);
171  for(UINT i=0; i<numDimensions; i++){
172  randomValues[i] = getRandomNumberUniform(minRange,maxRange);
173  }
174  return randomValues;
175 }
176 
177 VectorFloat Random::getRandomVectorGauss(UINT numDimensions,Float mu,Float sigma){
178  VectorFloat randomValues(numDimensions);
179  for(UINT i=0; i<numDimensions; i++){
180  randomValues[i] = getRandomNumberGauss(mu,sigma);
181  }
182  return randomValues;
183 }
184 
185 Vector< unsigned int > Random::getRandomSubset( const unsigned int startRange, const unsigned int endRange, const unsigned int subsetSize ){
186  unsigned int i = 0;
187  const unsigned int rangeSize = endRange - startRange;
188 
189  grt_assert( rangeSize > 0 );
190  grt_assert( endRange > startRange );
191  grt_assert( subsetSize <= rangeSize );
192 
193  Vector< unsigned int > indexs( rangeSize );
194  Vector< unsigned int > subset ( subsetSize );
195 
196  //Fill up the range buffer and the randomly suffle it
197  for(i=startRange; i<endRange; i++){
198  indexs[i] = i;
199  }
200  std::random_shuffle(indexs.begin(), indexs.end());
201 
202  //Select the first X values from the randomly shuffled range buffer as the subset
203  for(i=0; i<subsetSize; i++){
204  subset[i] = indexs[i];
205  }
206 
207  return subset;
208 }
209 
210 GRT_END_NAMESPACE
Definition: Timer.h:43
VectorFloat getRandomVectorGauss(UINT numDimensions, Float mu=0.0, Float sigma=1.0)
Definition: Random.cpp:177
Float getGauss(const Float &mu=0.0, const Float &sigma=1.0)
Definition: Random.cpp:146
Float getRandomNumberGauss(Float mu=0.0, Float sigma=1.0)
Definition: Random.cpp:142
Random(unsigned long long seed=0)
Definition: Random.cpp:29
Vector< unsigned int > getRandomSubset(const unsigned int startRange, const unsigned int endRange, const unsigned int subsetSize)
Definition: Random.cpp:185
int getRandomNumberWeighted(const Vector< int > &values, const VectorFloat &weights)
Definition: Random.cpp:68
bool setSeed(const unsigned long long seed=0)
Definition: Random.cpp:40
Float getRandomNumberUniform(Float minRange=0.0, Float maxRange=1.0)
Definition: Random.cpp:129
int getRandomNumberInt(int minRange, int maxRange)
Definition: Random.cpp:59
VectorFloat getRandomVectorUniform(UINT numDimensions, Float minRange=0.0, Float maxRange=1.0)
Definition: Random.cpp:169
Float getUniform(const Float &minRange=0.0, const Float &maxRange=1.0)
Definition: Random.cpp:133
~Random()
Definition: Random.cpp:38
static unsigned long getSystemTime()
Definition: Timer.h:211