IPSDK  4_1_0_2
IPSDK : Image Processing Software Development Kit
GenRandomValue.h
Go to the documentation of this file.
1 // GenRandomValue.h:
3 // -----------------
4 //
14 
15 #ifndef __IPSDKUTIL_GENRANDOMVALUE_H__
16 #define __IPSDKUTIL_GENRANDOMVALUE_H__
17 
19 #include <IPSDKUtil/BaseTypes.h>
22 
23 #include <boost/random/mersenne_twister.hpp>
24 #include <boost/random/discrete_distribution.hpp>
25 #include <boost/random/uniform_real_distribution.hpp>
26 #include <boost/type_traits/is_integral.hpp>
27 
28 namespace ipsdk {
29 
32 
40 {
42  template<typename T, typename Enable> friend struct GenRandomUniform;
43 
44  static boost::mt19937& getGen();
45  static void setSeed(ipUInt32 seed);
46  static void resetSeed();
47 };
48 
57  {
58  RandomUniformGenerator::setSeed(seed);
59  }
60 
62  {
63  RandomUniformGenerator::resetSeed();
64  }
65 
66 };
67 
70 template <typename T, typename Enable=void>
72 {
73 };
74 
75 template <typename T>
76 struct GenRandomUniform<T,
77  typename boost::enable_if_c<boost::is_integral<T>::value>::type>
78 {
79  static IPSDK_FORCEINLINE T act(const T& minVal, const T& maxVal)
80  {
81  boost::random::uniform_int_distribution<T> dist(minVal, maxVal);
82  return dist(RandomUniformGenerator::getGen());
83  }
84 
85  static IPSDK_FORCEINLINE T act()
86  {
88  }
89 };
90 
91 template <typename T>
93  typename boost::enable_if_c<!boost::is_integral<T>::value>::type>
94 {
95  static IPSDK_FORCEINLINE T act(const T& minVal, const T& maxVal)
96  {
97  // this test is a work-around for
98  // boost::random::uniform_real_distribution issue
99  // (boost 1.53 and prior)
100  if(maxVal / 2 - minVal / 2 > (NumericLimits<T>::max)() / 2) {
101  boost::random::uniform_real_distribution<T> dist(minVal/2, maxVal/2);
102  return dist(RandomUniformGenerator::getGen());
103  }
104  else {
105  boost::random::uniform_real_distribution<T> dist(minVal, maxVal);
106  return dist(RandomUniformGenerator::getGen());
107  }
108  }
109 
110  static IPSDK_FORCEINLINE T act()
111  {
113  }
114 };
115 
117 // (the seed of the random number generator is implicitly initialized to std::time(0))
118 template <typename T> IPSDK_FORCEINLINE T genRandomUniformValue(const T& minVal,
119  const T& maxVal)
120 {
121  return GenRandomUniform<T>::act(minVal, maxVal);
122 }
123 
125 // value for type T] (the seed of the random number generator is implicitly initialized to std::time(0))
126 template <typename T> IPSDK_FORCEINLINE T genRandomUniformValue()
127 {
128  return GenRandomUniform<T>::act();
129 }
130 
133 
134 } // end of namespace ipsdk
135 
136 #endif // __IPSDKUTIL_GENRANDOMVALUE_H__
Defines the IPSDK_FORCEINLINE.
IPSDK_FORCEINLINE T genRandomUniformValue(const T &minVal, const T &maxVal)
generates a random value of type T on the range [minVal, maxVal]
Definition: GenRandomValue.h:118
Main namespace for IPSDK library.
Definition: AlgorithmFunctionEfficiency.h:22
Definition: NumericLimits.h:27
Definition: DataItemNodeHdrMacrosDetails.h:48
Base types for multiplatform compatibility.
#define IPSDKUTIL_API
Import/Export macro for library IPSDKUtil.
Definition: IPSDKUtilExports.h:27
Definition of import/export macro for library.
class setting in its constructor the seed of the random number generator used by the implementation o...
Definition: GenRandomValue.h:55
class exposing the random number generator used by the implementation of the functions genRandomUnifo...
Definition: GenRandomValue.h:39
struct used by the implementation of the functions genRandomUniformValue
Definition: GenRandomValue.h:71
uint32_t ipUInt32
Base types definition.
Definition: BaseTypes.h:53