Image Component Library (ICL)
Random.h
Go to the documentation of this file.
1 /********************************************************************
2 ** Image Component Library (ICL) **
3 ** **
4 ** Copyright (C) 2006-2013 CITEC, University of Bielefeld **
5 ** Neuroinformatics Group **
6 ** Website: www.iclcv.org and **
7 ** http://opensource.cit-ec.de/projects/icl **
8 ** **
9 ** File : ICLUtils/src/ICLUtils/Random.h **
10 ** Module : ICLUtils **
11 ** Authors: Christof Elbrechter **
12 ** **
13 ** **
14 ** GNU LESSER GENERAL PUBLIC LICENSE **
15 ** This file may be used under the terms of the GNU Lesser General **
16 ** Public License version 3.0 as published by the **
17 ** **
18 ** Free Software Foundation and appearing in the file LICENSE.LGPL **
19 ** included in the packaging of this file. Please review the **
20 ** following information to ensure the license requirements will **
21 ** be met: http://www.gnu.org/licenses/lgpl-3.0.txt **
22 ** **
23 ** The development of this software was supported by the **
24 ** Excellence Cluster EXC 277 Cognitive Interaction Technology. **
25 ** The Excellence Cluster EXC 277 is a grant of the Deutsche **
26 ** Forschungsgemeinschaft (DFG) in the context of the German **
27 ** Excellence Initiative. **
28 ** **
29 ********************************************************************/
30 
31 #pragma once
32 
33 #include <ICLUtils/CompatMacros.h>
34 #include <ICLUtils/Time.h>
35 #include <ICLUtils/Range.h>
36 #include <ICLUtils/Exception.h>
37 #include <ICLUtils/ClippedCast.h>
38 #include <cmath>
39 #include <cstdlib>
40 #include <vector>
41 
42 #ifdef WIN32
43  #undef max
44 #endif
45 
46 namespace icl{
47  namespace utils{
49 
51  inline void randomSeed(long int seedval) {
52  #ifdef WIN32
53  srand(seedval);
54  #else
55  srand48(seedval);
56  #endif
57  }
58 
60  inline void randomSeed() { randomSeed(Time::now().toMicroSeconds()); }
61 
63 
64  struct RandomSeeder{
65  inline RandomSeeder(){randomSeed();}
66  };
67 
69  inline double random(double max = 1) {
70  #ifdef WIN32
71  // this generates quite poor random numbers, because RAND_MAX = 32767
72  return max*(static_cast<double>(rand()) / (1.0 + static_cast<double>(RAND_MAX)));
73  #else
74  return max*drand48();
75  #endif
76  }
77 
79 
82  inline double random(double min, double max) {
83  return ((max - min) * random() + min);
84  }
85 
87  template<class T>
88  inline double random(const Range<T> &r){
89  return random((double)r.minVal,(double)r.maxVal);
90  }
91 
93 
95  inline unsigned int random(unsigned int max) {
96  unsigned int val = static_cast<unsigned int>(floor(random (static_cast<double>(max)+1.0)));
97  return iclMin(val, max);
98  }
99 
100  #if 0
101  // removed due to lack of usebility: use std::fill(i.begin(c),i.end(c),URand(range)) instead
102 
104 
108  ICLUtils_API void random(ImgBase *poImage, const Range<double> &range=Range<double>(0,255), bool roiOnly=true);
109 
111 
117  ICLUtils_API void gaussRandom(ImgBase *poImage, double mean, double var, const Range<double> &minAndMax, bool roiOnly=true);
118  #endif
119 
125  ICLUtils_API double gaussRandom(double mean, double var);
126 
128 
134  inline double gaussRandom(double mean, double var, const Range<double> &range){
135  return clip<double>( gaussRandom(mean,var), range.minVal, range.maxVal);
136  }
137 
139 
154  class URand{
155  Range64f range;
156  public:
158  inline URand():range(0,1){};
159 
161  inline URand(icl64f min, icl64f max):range(min,max){}
162 
164  inline URand(const Range64f &range):range(range){};
165 
167  inline operator icl64f() const { return random(range.minVal,range.maxVal); }
168  };
169 
171 
172  class URandI{
173  unsigned int max;
174  public:
176  inline URandI(unsigned int max):max(max){}
177 
179  inline operator unsigned int() const{ return random(max); }
180  };
181 
183 
184  class GRand{
186  public:
188  inline GRand(icl64f mean=0, icl64f var=1.0):mean(mean),var(var){}
189 
191  inline operator icl64f() const { return gaussRandom(mean,var); }
192  };
193 
195 
196  class GRandClip{
198  Range64f range;
199  public:
201  inline GRandClip(icl64f mean, icl64f var,const Range64f &range):mean(mean),var(var),range(range){}
202 
204  inline operator icl64f() const { return gaussRandom(mean,var,range); }
205  };
206 
207 
208  /* }}} */
209 
210  inline std::vector<int> get_random_index_subset(int containerSize,
211  int subsetSize)
212  {
213  if(subsetSize > containerSize){
214  throw ICLException("get_random_index_subset: subsetsize must be <= containerSize");
215  }
216  std::vector<int> s(containerSize);
217  for(int i=0;i<containerSize;++i) s[i] = i;
218  std::random_shuffle(s.begin(), s.end());
219  return std::vector<int>(s.begin(), s.begin()+subsetSize);
220  }
221 
222  template<class T>
223  inline std::vector<T> get_random_subset(const std::vector<T> &s, int subsetSize)
224  {
225  std::vector<int> indices = get_random_index_subset(s.size(), subsetSize);
226  std::vector<T> subset(subsetSize);
227  for(int i=0;i<subsetSize;++i){
228  subset[i] = s[indices[i]];
229  }
230  return subset;
231  }
232 
233  template<class T>
234  inline void get_random_subset(const std::vector<T> &s, int subsetSize,
235  std::vector<T> &subset)
236  {
237  std::vector<int> indices = get_random_index_subset(s.size(), subsetSize);
238  subset.resize(subsetSize);
239  for(int i=0;i<subsetSize;++i){
240  subset[i] = s[indices[i]];
241  }
242  }
243 
244  template<class T>
245  inline void get_random_subset(const std::vector<T> &s, int subsetSize,
246  std::vector<T> &subset, std::vector<int> &indices)
247  {
248  indices = get_random_index_subset(s.size(), subsetSize);
249  subset.resize(subsetSize);
250  for(int i=0;i<subsetSize;++i){
251  subset[i] = s[indices[i]];
252  }
253  }
254 
255 
256 
257  } // namespace utils
258 }
259 
class representing a range defined by min and max value
Definition: Range.h:49
icl64f mean
Definition: Random.h:185
URandI(unsigned int max)
Create with given max value.
Definition: Random.h:176
Type maxVal
maximum value of this range
Definition: Range.h:92
undocument this line if you encounter any issues!
Definition: Any.h:37
void randomSeed(long int seedval)
Initilaize the random number generator.
Definition: Random.h:51
lightweight Random generator class for uniform random distributions in positive integer domain
Definition: Random.h:172
double mean(ForwardIterator begin, ForwardIterator end)
computes the mean value of a data range
Definition: MathFunctions.h:78
lightweight Random generator class for uniform random distributions
Definition: Random.h:154
#define ICLUtils_API
this macros are important for creating dll's
Definition: CompatMacros.h:171
GRandClip(icl64f mean, icl64f var, const Range64f &range)
Create with optionally given mean and variance.
Definition: Random.h:201
URand()
Range [0,1].
Definition: Random.h:158
std::vector< T > get_random_subset(const std::vector< T > &s, int subsetSize)
Definition: Random.h:223
#define iclMin(A, B)
Definition: Macros.h:204
std::vector< int > get_random_index_subset(int containerSize, int subsetSize)
Definition: Random.h:210
static Time now()
Ipp64f icl64f
64Bit floating point type for the ICL
Definition: BasicTypes.h:52
Type minVal
minimum value of this range
Definition: Range.h:89
icl64f var
Definition: Random.h:197
Range64f range
Definition: Random.h:155
GRand(icl64f mean=0, icl64f var=1.0)
Create with optionally given mean and variance.
Definition: Random.h:188
lightweight Random generator class for gaussian distributed numbers
Definition: Random.h:184
unsigned int max
Definition: Random.h:173
lightweight Random generator class for gaussian distributed numbers clipped to a given range
Definition: Random.h:196
ICLUtils_API double gaussRandom(double mean, double var)
Generate a gaussian random number with given mean and variance.
URand(icl64f min, icl64f max)
Given range.
Definition: Random.h:161
icl64f var
Definition: Random.h:185
Base class for Exception handling in the ICL.
Definition: Exception.h:42
icl64f mean
Definition: Random.h:197
Object based random seed caller.
Definition: Random.h:64
URand(const Range64f &range)
Given range.
Definition: Random.h:164
RandomSeeder()
Definition: Random.h:65
double random(double max=1)
Generates random numbers in range [0,1].
Definition: Random.h:69
Range64f range
Definition: Random.h:198